Skip to content

Commit 63bbee2

Browse files
committed
Refactorings and more exception message tests
1 parent 4c44e01 commit 63bbee2

8 files changed

+83
-49
lines changed

FluentAssertionsMvc.VisualState.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<VisualState xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ShowCheckBoxes="false">
33
<TopNode>[0-1000]G:\Dev\FluentAssertions.MVC\FluentAssertionsMvc.nunit</TopNode>
4-
<SelectedNode>[0-1074]FluentAssertions.Mvc3.Tests.RedirectToRoute_Tests.WithArea_GivenUnexpected_ShouldFail</SelectedNode>
4+
<SelectedNode>[0-1065]FluentAssertions.Mvc3.Tests.RedirectResultAssertions_Tests.WithPermanent_GivenUnexpectedUrl_ShouldFail</SelectedNode>
55
<ExcludeCategories>false</ExcludeCategories>
66
<Nodes>
77
<Node UniqueName="[0-1000]G:\Dev\FluentAssertions.MVC\FluentAssertionsMvc.nunit" Expanded="true" />
8-
<Node UniqueName="[0-1075]G:\Dev\FluentAssertions.MVC\bin\Debug\Test\FluentAssertions.Mvc3.Tests.dll" Expanded="true" />
9-
<Node UniqueName="[0-1076]FluentAssertions" Expanded="true" />
10-
<Node UniqueName="[0-1077]FluentAssertions.Mvc3" Expanded="true" />
11-
<Node UniqueName="[0-1078]FluentAssertions.Mvc3.Tests" Expanded="true" />
12-
<Node UniqueName="[0-1049]FluentAssertions.Mvc3.Tests.ActionResultAssertions_Tests" Expanded="true" />
8+
<Node UniqueName="[0-1079]G:\Dev\FluentAssertions.MVC\bin\Debug\Test\FluentAssertions.Mvc3.Tests.dll" Expanded="true" />
9+
<Node UniqueName="[0-1080]FluentAssertions" Expanded="true" />
10+
<Node UniqueName="[0-1081]FluentAssertions.Mvc3" Expanded="true" />
11+
<Node UniqueName="[0-1082]FluentAssertions.Mvc3.Tests" Expanded="true" />
12+
<Node UniqueName="[0-1066]FluentAssertions.Mvc3.Tests.ActionResultAssertions_Tests" Expanded="true" />
1313
<Node UniqueName="[0-1008]FluentAssertions.Mvc3.Tests.ContentResultAssertions_Tests" Expanded="true" />
14-
<Node UniqueName="[0-1047]FluentAssertions.Mvc3.Tests.RedirectResultAssertions_Tests" Expanded="true" />
15-
<Node UniqueName="[0-1062]FluentAssertions.Mvc3.Tests.RedirectToRoute_Tests" Expanded="true" />
14+
<Node UniqueName="[0-1061]FluentAssertions.Mvc3.Tests.RedirectResultAssertions_Tests" Expanded="true" />
15+
<Node UniqueName="[0-1048]FluentAssertions.Mvc3.Tests.RedirectToRoute_Tests" Expanded="true" />
1616
<Node UniqueName="[0-1033]FluentAssertions.Mvc3.Tests.RouteDataAssertions_Tests" Expanded="true" />
1717
<Node UniqueName="[0-1001]FluentAssertions.Mvc3.Tests.RouteValueDictionary_Extensions_Tests" Expanded="true" />
1818
<Node UniqueName="[0-1015]FluentAssertions.Mvc3.Tests.ViewResultAssertions_Tests" Expanded="true" />

src/FluentAssertions.Mvc3/ContentResultAssertions.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@
77

88
namespace FluentAssertions.Mvc3
99
{
10-
public class ContentResultAssertions : ReferenceTypeAssertions<ContentResult, ContentResultAssertions>
10+
public class ContentResultAssertions : ObjectAssertions
1111
{
1212
class Constants
1313
{
1414
public const string CommonFailMessage = "Expect {0} to be '{1}' but was '{2}'";
1515
}
1616

17-
public ContentResultAssertions(ContentResult subject)
17+
public ContentResultAssertions(ContentResult subject) : base(subject)
1818
{
19-
Subject = subject;
2019
}
2120

2221
public ContentResultAssertions WithContent(string expectedContent)
@@ -27,10 +26,12 @@ public ContentResultAssertions WithContent(string expectedContent)
2726

2827
public ContentResultAssertions WithContent(string expectedContent, string reason, params object[] reasonArgs)
2928
{
29+
string actualContent = (Subject as ContentResult).Content;
30+
3031
Execute.Verification
31-
.ForCondition(string.Equals(Subject.Content, expectedContent, StringComparison.InvariantCultureIgnoreCase))
32+
.ForCondition(string.Equals(actualContent, expectedContent, StringComparison.InvariantCultureIgnoreCase))
3233
.BecauseOf(reason, reasonArgs)
33-
.FailWith(string.Format(Constants.CommonFailMessage, "ContentResult.Content", expectedContent, Subject.Content));
34+
.FailWith(string.Format(Constants.CommonFailMessage, "ContentResult.Content", expectedContent, actualContent));
3435

3536
return this;
3637
}
@@ -43,10 +44,12 @@ public ContentResultAssertions WithContentType(string expectedContent)
4344

4445
public ContentResultAssertions WithContentType(string expectedContent, string reason, params object[] reasonArgs)
4546
{
47+
string actualContentType = (Subject as ContentResult).ContentType;
48+
4649
Execute.Verification
47-
.ForCondition(string.Equals(expectedContent, Subject.ContentType, StringComparison.InvariantCultureIgnoreCase))
50+
.ForCondition(string.Equals(expectedContent, actualContentType, StringComparison.InvariantCultureIgnoreCase))
4851
.BecauseOf(reason, reasonArgs)
49-
.FailWith(Constants.CommonFailMessage, "ContentResult.ContentType", expectedContent, Subject.ContentType);
52+
.FailWith(Constants.CommonFailMessage, "ContentResult.ContentType", expectedContent, actualContentType);
5053

5154
return this;
5255
}
@@ -59,10 +62,12 @@ public ContentResultAssertions WithContentEncoding(Encoding expectedEncoding)
5962

6063
public ContentResultAssertions WithContentEncoding(Encoding expectedEncoding, string reason, params object[] reasonArgs)
6164
{
65+
Encoding actualContentEncoding = (Subject as ContentResult).ContentEncoding;
66+
6267
Execute.Verification
63-
.ForCondition(expectedEncoding == Subject.ContentEncoding)
68+
.ForCondition(expectedEncoding == actualContentEncoding)
6469
.BecauseOf(reason, reasonArgs)
65-
.FailWith(Constants.CommonFailMessage, "ContentResult.ContentType", expectedEncoding.ToString(), Subject.ContentEncoding.ToString());
70+
.FailWith(Constants.CommonFailMessage, "ContentResult.ContentType", expectedEncoding.ToString(), actualContentEncoding.ToString());
6671

6772
return this;
6873
}

src/FluentAssertions.Mvc3/PartialViewResultAssertions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
using System.Text;
55
using System.Web.Mvc;
66
using System.Diagnostics;
7+
using FluentAssertions.Assertions;
78

89
namespace FluentAssertions.Mvc3
910
{
1011
[DebuggerNonUserCode]
11-
public class PartialViewResultAssertions : ViewResultBaseAssertions<PartialViewResult>
12+
public class PartialViewResultAssertions : ObjectAssertions
1213
{
1314
public PartialViewResultAssertions(PartialViewResult viewResult) : base(viewResult) { }
1415
}

src/FluentAssertions.Mvc3/ViewResultAssertions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ public ViewResultAssertions WithMasterName(string expectedMasterName)
1818

1919
public ViewResultAssertions WithMasterName(string expectedMasterName, string reason, params object[] reasonArgs)
2020
{
21+
string actualMasterName = (Subject as ViewResult).MasterName;
22+
2123
Execute.Verification
22-
.ForCondition(string.Equals(expectedMasterName, Subject.MasterName, StringComparison.InvariantCultureIgnoreCase))
24+
.ForCondition(string.Equals(expectedMasterName, actualMasterName, StringComparison.InvariantCultureIgnoreCase))
2325
.BecauseOf(reason, reasonArgs)
24-
.FailWith("Expected MasterName to be '{0}' but was '{1}'", expectedMasterName, Subject.MasterName);
26+
.FailWith("Expected MasterName to be '{0}' but was '{1}'", expectedMasterName, actualMasterName);
2527
return this;
2628
}
2729
}

src/FluentAssertions.Mvc3/ViewResultBaseAssertions.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
namespace FluentAssertions.Mvc3
1010
{
1111
[DebuggerNonUserCode]
12-
public abstract class ViewResultBaseAssertions<T> : ReferenceTypeAssertions<T, ViewResultBaseAssertions<T>>
12+
public abstract class ViewResultBaseAssertions<T> : ObjectAssertions
1313
where T : ViewResultBase
1414
{
15-
public ViewResultBaseAssertions(ViewResultBase subject)
15+
public ViewResultBaseAssertions(ViewResultBase subject) : base(subject)
1616
{
1717
Subject = (T)subject;
1818
}
@@ -25,10 +25,12 @@ public ViewResultBaseAssertions<T> WithViewName(string expectedViewName)
2525

2626
public ViewResultBaseAssertions<T> WithViewName(string expectedViewName, string reason, params object[] reasonArgs)
2727
{
28+
string actualViewName = (Subject as ViewResultBase).ViewName;
29+
2830
Execute.Verification
29-
.ForCondition(string.Equals(expectedViewName, Subject.ViewName, StringComparison.InvariantCultureIgnoreCase))
31+
.ForCondition(string.Equals(expectedViewName, actualViewName, StringComparison.InvariantCultureIgnoreCase))
3032
.BecauseOf(reason, reasonArgs)
31-
.FailWith("Expected ViewName to be '{0}' but was '{1}'", expectedViewName, Subject.ViewName);
33+
.FailWith("Expected ViewName to be '{0}' but was '{1}'", expectedViewName, actualViewName);
3234
return this;
3335
}
3436

@@ -40,12 +42,14 @@ public ViewResultBaseAssertions<T> WithViewData(string key, object expectedValue
4042

4143
public ViewResultBaseAssertions<T> WithViewData(string key, object expectedValue, string reason, params object[] reasonArgs)
4244
{
45+
ViewDataDictionary actualViewData = (Subject as ViewResultBase).ViewData;
46+
4347
Execute.Verification
44-
.ForCondition(Subject.ViewData.ContainsKey(key))
48+
.ForCondition(actualViewData.ContainsKey(key))
4549
.BecauseOf(reason, reasonArgs)
4650
.FailWith("ViewData does not contain key of '{0}'", key);
4751

48-
Subject.ViewData[key].Should().Be(expectedValue);
52+
actualViewData[key].Should().Be(expectedValue);
4953

5054
return this;
5155
}
@@ -58,12 +62,14 @@ public ViewResultBaseAssertions<T> WithTempData(string key, object expectedValue
5862

5963
public ViewResultBaseAssertions<T> WithTempData(string key, object expectedValue, string reason, params object[] reasonArgs)
6064
{
65+
TempDataDictionary actualTempData = (Subject as ViewResultBase).TempData;
66+
6167
Execute.Verification
62-
.ForCondition(Subject.TempData.ContainsKey(key))
68+
.ForCondition(actualTempData.ContainsKey(key))
6369
.BecauseOf(reason, reasonArgs)
6470
.FailWith("TempData does not contain key of '{0}'", key);
6571

66-
Subject.TempData[key].Should().Be(expectedValue);
72+
actualTempData[key].Should().Be(expectedValue);
6773

6874
return this;
6975
}
@@ -72,17 +78,20 @@ public object Model
7278
{
7379
get
7480
{
75-
return Subject.Model;
81+
var model = (Subject as ViewResult).Model;
82+
return model;
7683
}
7784
}
7885

7986
public TModel ModelAs<TModel>()
8087
{
88+
object model = (Subject as ViewResultBase).Model;
89+
8190
Execute.Verification
82-
.ForCondition(Subject.Model is TModel)
83-
.FailWith("Expected Model to be of type '{0}' but was '{1}'", typeof(TModel).Name, Subject.Model.GetType().Name);
91+
.ForCondition(model is TModel)
92+
.FailWith("Expected Model to be of type '{0}' but was '{1}'", typeof(TModel).Name, model.GetType().Name);
8493

85-
return (TModel)Subject.Model;
94+
return (TModel)model;
8695
}
8796
}
8897
}

tests/FluentAssertions.Mvc3.Tests/ContentResultAssertions_Tests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public void WithContent_GivenUnexpected_ShouldFail()
2222
{
2323
ActionResult result = new ContentResult { Content = "content" };
2424
Action a = () => result.Should().BeContent().WithContent("xyz");
25-
a.ShouldThrow<Exception>();
25+
a.ShouldThrow<Exception>()
26+
.WithMessage("");
2627
}
2728

2829
[Test]
@@ -37,7 +38,8 @@ public void WithContentType_GivenUnexpected_ShouldFail()
3738
{
3839
ActionResult result = new ContentResult { ContentType = "text/html" };
3940
Action a = () => result.Should().BeContent().WithContentType("xyz");
40-
a.ShouldThrow<Exception>();
41+
a.ShouldThrow<Exception>()
42+
.WithMessage("");
4143
}
4244

4345
[Test]
@@ -52,7 +54,8 @@ public void WithContentEncoding_GivenUnexpected_ShouldFail()
5254
{
5355
ActionResult result = new ContentResult { ContentEncoding = Encoding.ASCII };
5456
Action a = () => result.Should().BeContent().WithContentEncoding(Encoding.Unicode);
55-
a.ShouldThrow<Exception>();
57+
a.ShouldThrow<Exception>()
58+
.WithMessage("");
5659
}
5760
}
5861
}

tests/FluentAssertions.Mvc3.Tests/RouteDataAssertions_Tests.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public void HaveValue_GivenKeyDoesExist_ShouldFail()
4040
{
4141
var routeData = _routes.GetRouteDataForUrl("/accounts/check");
4242
Action a = () => routeData.Should().HaveValue("xyz", "");
43-
a.ShouldThrow<Exception>();
43+
a.ShouldThrow<Exception>()
44+
.WithMessage("");
4445
}
4546

4647
[Test]
@@ -55,7 +56,8 @@ public void HaveValue_GivenUnexpectedController_ShouldFail()
5556
{
5657
var routeData = _routes.GetRouteDataForUrl("/accounts/check");
5758
Action a = () => routeData.Should().HaveValue("controller", "xyz");
58-
a.ShouldThrow<Exception>();
59+
a.ShouldThrow<Exception>()
60+
.WithMessage("");
5961
}
6062

6163
[Test]
@@ -77,7 +79,8 @@ public void HaveValue_GivenUnexpectedId_ShouldFail()
7779
{
7880
var routeData = _routes.GetRouteDataForUrl("/accounts/check/44");
7981
Action a = () => routeData.Should().HaveValue("id", "999");
80-
a.ShouldThrow<Exception>();
82+
a.ShouldThrow<Exception>()
83+
.WithMessage("");
8184
}
8285

8386
[Test]
@@ -92,7 +95,8 @@ public void HaveController_GivenUnexpectedValue_ShouldFail()
9295
{
9396
var routeData = _routes.GetRouteDataForUrl("/accounts/check/44");
9497
Action a = () => routeData.Should().HaveController("xyz");
95-
a.ShouldThrow<Exception>();
98+
a.ShouldThrow<Exception>()
99+
.WithMessage("");
96100
}
97101

98102
[Test]
@@ -107,15 +111,17 @@ public void HaveAction_GivenUnexpectedValue_ShouldFail()
107111
{
108112
var routeData = _routes.GetRouteDataForUrl("/accounts/check/44");
109113
Action a = () => routeData.Should().HaveAction("xyz");
110-
a.ShouldThrow<Exception>();
114+
a.ShouldThrow<Exception>()
115+
.WithMessage("");
111116
}
112117

113118
[Test]
114119
public void HaveDataToken_GivenKeyDoesExist_ShouldFail()
115120
{
116121
var routeData = _routes.GetRouteDataForUrl("/accounts/check");
117122
Action a = () => routeData.Should().HaveDataToken("xyz", "");
118-
a.ShouldThrow<Exception>();
123+
a.ShouldThrow<Exception>()
124+
.WithMessage("");
119125
}
120126

121127
[Test]
@@ -130,7 +136,8 @@ public void HaveDataToken_GivenUnexpectedArea_ShouldFail()
130136
{
131137
var routeData = _routes.GetRouteDataForUrl("/accounts/check");
132138
Action a = () => routeData.Should().HaveDataToken("area", "xyz");
133-
a.ShouldThrow<Exception>();
139+
a.ShouldThrow<Exception>()
140+
.WithMessage("");
134141
}
135142
}
136143
}

tests/FluentAssertions.Mvc3.Tests/ViewResultAssertions_Tests.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public void WithMasterName_GivenUnexpectedValue_ShouldFail()
3434
};
3535

3636
Action action = () => result.Should().BeView().WithMasterName("xyz");
37-
action.ShouldThrow<Exception>();
37+
action.ShouldThrow<Exception>()
38+
.WithMessage("");
3839
}
3940

4041
[Test]
@@ -57,7 +58,8 @@ public void WithViewName_GivenUnexpectedValue_ShouldFail()
5758
};
5859

5960
Action action = () => result.Should().BeView().WithViewName("xyz");
60-
action.ShouldThrow<Exception>();
61+
action.ShouldThrow<Exception>()
62+
.WithMessage("");
6163
}
6264

6365
[Test]
@@ -149,7 +151,8 @@ public void WithViewData_GivenUnexpectedValue_ShouldFail()
149151
};
150152

151153
Action a = () => result.Should().BeView().WithViewData("key1", "xyz");
152-
a.ShouldThrow<Exception>();
154+
a.ShouldThrow<Exception>()
155+
.WithMessage("");
153156
}
154157

155158
[Test]
@@ -161,7 +164,8 @@ public void WithViewData_GivenUnexpectedKey_ShouldFail()
161164
};
162165

163166
Action a = () => result.Should().BeView().WithViewData("xyz", "value1");
164-
a.ShouldThrow<Exception>();
167+
a.ShouldThrow<Exception>()
168+
.WithMessage("");
165169
}
166170

167171
[Test]
@@ -184,7 +188,8 @@ public void Model_GivenUnexpectedValue_ShouldFail()
184188
};
185189

186190
Action a = () => result.Should().BeView().Model.Should().Be("xyx");
187-
a.ShouldThrow<Exception>();
191+
a.ShouldThrow<Exception>()
192+
.WithMessage("");
188193
}
189194

190195
[Test]
@@ -207,7 +212,8 @@ public void ModelAs_GivenUnexpectedValue_ShouldFail()
207212
};
208213

209214
Action a = () => result.Should().BeView().ModelAs<string>().Should().Be("xyx");
210-
a.ShouldThrow<Exception>();
215+
a.ShouldThrow<Exception>()
216+
.WithMessage("");
211217
}
212218

213219
[Test]
@@ -219,7 +225,8 @@ public void ModelAs_GivenWrongType_ShouldFail()
219225
};
220226

221227
Action a = () => result.Should().BeView().ModelAs<int>().Should().Be(2);
222-
a.ShouldThrow<Exception>();
228+
a.ShouldThrow<Exception>()
229+
.WithMessage("");
223230
}
224231
}
225232
}

0 commit comments

Comments
 (0)