Skip to content

Commit d9af2d0

Browse files
committed
Completed RedirectToRouteAssertions and tests
1 parent 49bb9bf commit d9af2d0

File tree

3 files changed

+139
-35
lines changed

3 files changed

+139
-35
lines changed

_todo_.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
[ ] Check for ActionResult types that derive from basic types
22
[ ] Change lib folder to use restorable nuget packages
3+
[ ] All ShouldThrow tests must assert the exception message
4+
[ ] Fix all fail messages to be like RedirectToRouteAssertions
5+
[ ] Change all dictionary asserts to us Subject.RouteValues.Should().Contain
6+
[ ] FluentAssertions should *not* blow up if given null reasonArgs and a reason
7+
[ ] Should because syntax in sample code
8+
[ ] XML comments every where

src/FluentAssertions.Mvc3/RedirectToRouteAssertions.cs

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ namespace FluentAssertions.Mvc3
1010
{
1111
public class RedirectToRouteAssertions : ReferenceTypeAssertions<RedirectToRouteResult, RedirectToRouteAssertions>
1212
{
13-
public class Constants
14-
{
15-
public const string CommonFailMessage = "Expected RedirectResult.{0} to be '{1}' but was '{2}'";
16-
}
17-
1813
public RedirectToRouteAssertions(RedirectToRouteResult subject)
1914
{
2015
Subject = subject;
@@ -29,10 +24,9 @@ public RedirectToRouteAssertions WithPermanent(bool expectedPermanent)
2924
public RedirectToRouteAssertions WithPermanent(bool expectedPermanent, string reason, params object[] reasonArgs)
3025
{
3126
Execute.Verification
32-
.ForCondition(expectedPermanent == Subject.Permanent)
3327
.BecauseOf(reason, reasonArgs)
34-
.FailWith(Constants.CommonFailMessage, "Permanent", expectedPermanent, Subject.Permanent);
35-
28+
.ForCondition(expectedPermanent == Subject.Permanent)
29+
.FailWith("Expected RedirectToRoute.Permanent to be {0}{reason}, but found {1}", expectedPermanent, Subject.Permanent);
3630
return this;
3731
}
3832

@@ -45,13 +39,25 @@ public RedirectToRouteAssertions WithRouteName(string expectedRouteName)
4539
public RedirectToRouteAssertions WithRouteName(string expectedRouteName, string reason, params object[] reasonArgs)
4640
{
4741
Execute.Verification
48-
.ForCondition(string.Equals(expectedRouteName, Subject.RouteName, StringComparison.InvariantCultureIgnoreCase))
4942
.BecauseOf(reason, reasonArgs)
50-
.FailWith(Constants.CommonFailMessage, "RouteName", expectedRouteName, Subject.Permanent);
43+
.ForCondition(string.Equals(expectedRouteName, Subject.RouteName, StringComparison.InvariantCultureIgnoreCase))
44+
.FailWith("Expected RedirectToRoute.RouteName to be {0}{reason}, but found {1}", expectedRouteName, Subject.RouteName);
5145

5246
return this;
5347
}
5448

49+
public RedirectToRouteAssertions WithRouteValue(string key, object expectedValue)
50+
{
51+
WithRouteValue(key, expectedValue, string.Empty, null);
52+
return this;
53+
}
54+
55+
public RedirectToRouteAssertions WithRouteValue(string key, object expectedValue, string reason, params object[] reasonArgs)
56+
{
57+
Subject.RouteValues.Should().Contain(new KeyValuePair<string, object>(key, expectedValue), reason, reasonArgs);
58+
return this;
59+
}
60+
5561
public RedirectToRouteAssertions WithController(string expectedControllerName)
5662
{
5763
WithController(expectedControllerName, string.Empty, null);
@@ -60,27 +66,31 @@ public RedirectToRouteAssertions WithController(string expectedControllerName)
6066

6167
public RedirectToRouteAssertions WithController(string expectedControllerName, string reason, params object[] reasonArgs)
6268
{
63-
//Subject.RouteValues[""]
64-
69+
WithRouteValue("Controller", expectedControllerName, reason, reasonArgs);
6570
return this;
6671
}
6772

68-
public RedirectToRouteAssertions WithRouteValue(string key, object expectedValue)
73+
public RedirectToRouteAssertions WithAction(string expectedAction)
6974
{
70-
WithRouteValue(key, expectedValue, string.Empty, null);
75+
WithAction(expectedAction, string.Empty, null);
7176
return this;
7277
}
7378

74-
public RedirectToRouteAssertions WithRouteValue(string key, object expectedValue, string reason, params object[] reasonArgs)
79+
public RedirectToRouteAssertions WithAction(string expectedArea, string reason, params object[] reasonArgs)
7580
{
76-
Execute.Verification
77-
.ForCondition(Subject.RouteValues.ContainsKey(key))
78-
.BecauseOf(reason, reasonArgs)
79-
.FailWith(string.Format("RedirectResult.RouteValues does not contain key '{0}'", key));
80-
81-
var actualValue = Subject.RouteValues[key];
82-
actualValue.Should().Be(expectedValue);
81+
WithRouteValue("Action", expectedArea, reason, reasonArgs);
82+
return this;
83+
}
8384

85+
public RedirectToRouteAssertions WithArea(string expectedAction)
86+
{
87+
WithArea(expectedAction, string.Empty, null);
88+
return this;
89+
}
90+
91+
public RedirectToRouteAssertions WithArea(string expectedArea, string reason, params object[] reasonArgs)
92+
{
93+
WithRouteValue("Area", expectedArea, reason, reasonArgs);
8494
return this;
8595
}
8696
}

tests/FluentAssertions.Mvc3.Tests/RedirectToRoute_Tests.cs

Lines changed: 101 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ public void WithPermanent_GivenExpected_ShouldPass()
1818
}
1919

2020
[Test]
21-
public void WithPermanent_GivenUnxpected_ShouldFail()
21+
public void WithPermanent_GivenUnExpected_ShouldFail()
2222
{
2323
ActionResult result = new RedirectToRouteResult("", null, true);
2424
Action a = () => result.Should()
2525
.BeRedirectToRoute()
2626
.WithPermanent(false);
27-
a.ShouldThrow<Exception>();
27+
a.ShouldThrow<Exception>()
28+
.WithMessage("Expected RedirectToRoute.Permanent to be False, but found True");
2829
}
2930

3031
[Test]
@@ -37,47 +38,134 @@ public void WithRouteName_GivenExpected_ShouldPass()
3738
}
3839

3940
[Test]
40-
public void WithRouteName_GivenUnxpected_ShouldFail()
41+
public void WithRouteName_GivenUnExpected_ShouldFail()
4142
{
4243
ActionResult result = new RedirectToRouteResult("default", null);
4344
Action a = () => result.Should()
4445
.BeRedirectToRoute()
4546
.WithRouteName("xyz");
46-
a.ShouldThrow<Exception>();
47+
a.ShouldThrow<Exception>()
48+
.WithMessage("Expected RedirectToRoute.RouteName to be \"xyz\", but found \"default\"");
4749
}
4850

4951
[Test]
50-
public void WithController_GivenExpected_ShouldPass()
52+
public void WithRouteValue_GivenExpected_ShouldPass()
5153
{
5254
ActionResult result = new RedirectToRouteResult("", new RouteValueDictionary(
53-
new {
54-
Controller = "home",
55-
Action = "list",
55+
new
56+
{
5657
Id = "22"
5758
}));
5859

5960
result.Should()
6061
.BeRedirectToRoute()
61-
.WithController("Home");
62+
.WithRouteValue("Id", "22");
6263
}
6364

6465
[Test]
65-
public void WithRouteValue_GivenUnxpected_ShouldFail()
66+
public void WithRouteValue_GivenUnexpected_ShouldFail()
6667
{
6768
ActionResult result = new RedirectToRouteResult("", new RouteValueDictionary(
6869
new
6970
{
70-
Controller = "home",
71-
Action = "list",
7271
Id = "22"
7372
}));
7473

74+
Action a = () => result.Should()
75+
.BeRedirectToRoute()
76+
.WithRouteValue("Id", "11");
77+
a.ShouldThrow<Exception>()
78+
.WithMessage("Expected dictionary to contain value \"11\" at key \"Id\", but found \"22\".");
79+
}
80+
81+
[Test]
82+
public void WithController_GivenExpected_ShouldPass()
83+
{
84+
ActionResult result = new RedirectToRouteResult("", new RouteValueDictionary(
85+
new
86+
{
87+
Controller = "home"
88+
}));
89+
90+
result.Should()
91+
.BeRedirectToRoute()
92+
.WithController("home");
93+
}
94+
95+
[Test]
96+
public void WithController_GivenUnexpected_ShouldFail()
97+
{
98+
ActionResult result = new RedirectToRouteResult("", new RouteValueDictionary(
99+
new
100+
{
101+
Controller = "home"
102+
}));
103+
75104
Action a = () => result.Should()
76105
.BeRedirectToRoute()
77106
.WithController("xyz");
78-
a.ShouldThrow<Exception>();
107+
a.ShouldThrow<Exception>()
108+
.WithMessage("Expected dictionary to contain value \"xyz\" at key \"Controller\", but found \"home\".");
109+
}
110+
111+
[Test]
112+
public void WithAction_GivenExpected_ShouldPass()
113+
{
114+
ActionResult result = new RedirectToRouteResult("", new RouteValueDictionary(
115+
new
116+
{
117+
Action = "index"
118+
}));
119+
120+
result.Should()
121+
.BeRedirectToRoute()
122+
.WithAction("index");
123+
}
124+
125+
[Test]
126+
public void WithAction_GivenUnexpected_ShouldFail()
127+
{
128+
ActionResult result = new RedirectToRouteResult("", new RouteValueDictionary(
129+
new
130+
{
131+
Action = "index"
132+
}));
133+
134+
Action a = () => result.Should()
135+
.BeRedirectToRoute()
136+
.WithAction("xyz");
137+
a.ShouldThrow<Exception>()
138+
.WithMessage("Expected dictionary to contain value \"xyz\" at key \"Action\", but found \"index\".");
139+
}
140+
141+
[Test]
142+
public void WithArea_GivenExpected_ShouldPass()
143+
{
144+
ActionResult result = new RedirectToRouteResult("", new RouteValueDictionary(
145+
new
146+
{
147+
Area = "accounts"
148+
}));
149+
150+
result.Should()
151+
.BeRedirectToRoute()
152+
.WithArea("accounts");
79153
}
80154

155+
[Test]
156+
public void WithArea_GivenUnexpected_ShouldFail()
157+
{
158+
ActionResult result = new RedirectToRouteResult("", new RouteValueDictionary(
159+
new
160+
{
161+
Area = "accounts"
162+
}));
81163

164+
Action a = () => result.Should()
165+
.BeRedirectToRoute()
166+
.WithArea("xyz");
167+
a.ShouldThrow<Exception>()
168+
.WithMessage("Expected dictionary to contain value \"xyz\" at key \"Area\", but found \"accounts\".");
169+
}
82170
}
83171
}

0 commit comments

Comments
 (0)