Skip to content

Commit 9c0ca24

Browse files
committed
Fixed ViewResultBaseAssertions
ViewResultBase doesn't exist anymore in the AspNetCore.Mvc framework. This meant that a huge number of modification were needed to make the ViewResultBaseAssertions class work correctly. Instead of polluting the whole class with tons of #if and to rename it to ViewResultAssertions, I conditionally removed the whole ViewResultBaseAssertions class and created a ViewResultAssertions only for the .Net Core project.
1 parent cf39340 commit 9c0ca24

File tree

4 files changed

+4406
-586
lines changed

4 files changed

+4406
-586
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
using System;
2+
using System.Diagnostics;
3+
using FluentAssertions.Execution;
4+
using FluentAssertions.Mvc;
5+
using FluentAssertions.Primitives;
6+
using Microsoft.AspNetCore.Mvc;
7+
8+
namespace FluentAssertions.AspNetCore.Mvc
9+
{
10+
/// <summary>
11+
/// Contains a number of methods to assert that a <see cref="ViewResult" /> is in the expected state.
12+
/// </summary>
13+
[DebuggerNonUserCode]
14+
public class ViewResultAssertions<T> : ObjectAssertions
15+
where T : ViewResult
16+
{
17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="T:ViewResultAssertions" /> class.
19+
/// </summary>
20+
/// <param name="subject">The object to test assertion on</param>
21+
public ViewResultAssertions(ViewResult subject) : base(subject)
22+
{
23+
}
24+
25+
private ViewResult ViewResultSubject => (ViewResult) Subject;
26+
27+
/// <summary>
28+
/// The model.
29+
/// </summary>
30+
public object Model
31+
{
32+
get
33+
{
34+
var model = ViewResultSubject.Model;
35+
return model;
36+
}
37+
}
38+
39+
/// <summary>
40+
/// Asserts that the view name is the expected view name.
41+
/// </summary>
42+
/// <param name="expectedViewName">The view name.</param>
43+
/// <param name="reason">
44+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
45+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
46+
/// </param>
47+
/// <param name="reasonArgs">
48+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
49+
/// </param>
50+
public ViewResultAssertions<T> WithViewName(string expectedViewName, string reason = "",
51+
params object[] reasonArgs)
52+
{
53+
var actualViewName = ViewResultSubject.ViewName;
54+
55+
Execute.Assertion
56+
.ForCondition(string.Equals(expectedViewName, actualViewName, StringComparison.OrdinalIgnoreCase))
57+
.BecauseOf(reason, reasonArgs)
58+
.FailWith(FailureMessages.ViewResultBase_ViewName, expectedViewName, actualViewName);
59+
return this;
60+
}
61+
62+
/// <summary>
63+
/// Asserts that the view data contains the expected data.
64+
/// </summary>
65+
/// <param name="key">The expected view data key.</param>
66+
/// <param name="expectedValue">The expected view data.</param>
67+
/// <param name="reason">
68+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
69+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
70+
/// </param>
71+
/// <param name="reasonArgs">
72+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
73+
/// </param>
74+
public ViewResultAssertions<T> WithViewData(string key, object expectedValue, string reason = "",
75+
params object[] reasonArgs)
76+
{
77+
var actualViewData = ViewResultSubject.ViewData;
78+
79+
Execute.Assertion
80+
.ForCondition(actualViewData.ContainsKey(key))
81+
.BecauseOf(reason, reasonArgs)
82+
.FailWith(FailureMessages.ViewResultBase_ViewData_ContainsKey, key);
83+
84+
var actualValue = actualViewData[key];
85+
86+
Execute.Assertion
87+
.ForCondition(actualValue.Equals(expectedValue))
88+
.BecauseOf(reason, reasonArgs)
89+
.FailWith(FailureMessages.ViewResultBase_ViewData_HaveValue, key, expectedValue, actualValue);
90+
91+
return this;
92+
}
93+
94+
/// <summary>
95+
/// Asserts that the temp data contains the expected data.
96+
/// </summary>
97+
/// <param name="key">The expected temp data key.</param>
98+
/// <param name="expectedValue">The expected temp data.</param>
99+
/// <param name="reason">
100+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
101+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
102+
/// </param>
103+
/// <param name="reasonArgs">
104+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
105+
/// </param>
106+
public ViewResultAssertions<T> WithTempData(string key, object expectedValue, string reason = "",
107+
params object[] reasonArgs)
108+
{
109+
var actualTempData = ViewResultSubject.TempData;
110+
111+
Execute.Assertion
112+
.ForCondition(actualTempData.ContainsKey(key))
113+
.BecauseOf(reason, reasonArgs)
114+
.FailWith("TempData does not contain key of '{0}'", key);
115+
116+
actualTempData[key].Should().Be(expectedValue);
117+
118+
return this;
119+
}
120+
121+
/// <summary>
122+
/// Asserts the model is of the expected type.
123+
/// </summary>
124+
/// <typeparam name="TModel">The expected type.</typeparam>
125+
/// <returns>The typed model.</returns>
126+
public TModel ModelAs<TModel>()
127+
{
128+
var model = ViewResultSubject.Model;
129+
130+
if (model == null)
131+
Execute.Assertion.FailWith(FailureMessages.ViewResultBase_NullModel, typeof(TModel).Name);
132+
133+
Execute.Assertion
134+
.ForCondition(model is TModel)
135+
.FailWith("Expected Model to be of type '{0}' but was '{1}'", typeof(TModel).Name, model.GetType().Name);
136+
137+
return (TModel) model;
138+
}
139+
140+
/// <summary>
141+
/// Asserts that the default view will be used.
142+
/// </summary>
143+
/// <param name="reason">
144+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
145+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
146+
/// </param>
147+
/// <param name="reasonArgs">
148+
/// Zero or more objects to format using the placeholders in <see cref="reason" />.
149+
/// </param>
150+
public ViewResultAssertions<T> WithDefaultViewName(string reason = "", params object[] reasonArgs)
151+
{
152+
var viewName = ViewResultSubject.ViewName;
153+
154+
Execute.Assertion
155+
.ForCondition(viewName == string.Empty)
156+
.BecauseOf(reason, reasonArgs)
157+
.FailWith(FailureMessages.ViewResultBase_WithDefaultViewName, viewName);
158+
159+
return this;
160+
}
161+
}
162+
}

src/FluentAssertions.AspNetCore.Mvc/project.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
},
1111

1212
"dependencies": {
13+
"FluentAssertions": "4.13.0",
14+
"Microsoft.AspNetCore.Mvc": "1.0.0",
1315
"NETStandard.Library": "1.6.0"
1416
},
1517

0 commit comments

Comments
 (0)