Skip to content

Commit fdbf3c9

Browse files
committed
Created a new PartialViewResultAssertions
Since `ViewResultBase` is no more, we can't use the same assertion class for `ViewResult` and `PartialViewResult`. I had to recreate a special class just for `PartialViewResult` in .Net Core project.
1 parent 9c0ca24 commit fdbf3c9

File tree

3 files changed

+155
-11
lines changed

3 files changed

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

src/FluentAssertions.AspNetCore.Mvc/ViewResultAssertions.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,16 @@ public class ViewResultAssertions<T> : ObjectAssertions
1818
/// Initializes a new instance of the <see cref="T:ViewResultAssertions" /> class.
1919
/// </summary>
2020
/// <param name="subject">The object to test assertion on</param>
21-
public ViewResultAssertions(ViewResult subject) : base(subject)
21+
public ViewResultAssertions(T subject) : base(subject)
2222
{
2323
}
2424

25-
private ViewResult ViewResultSubject => (ViewResult) Subject;
25+
private T ViewResultSubject => (T) Subject;
2626

2727
/// <summary>
2828
/// The model.
2929
/// </summary>
30-
public object Model
31-
{
32-
get
33-
{
34-
var model = ViewResultSubject.Model;
35-
return model;
36-
}
37-
}
30+
public object Model => ViewResultSubject.Model;
3831

3932
/// <summary>
4033
/// Asserts that the view name is the expected view name.

src/FluentAssertions.Mvc.Shared/PartialViewResultAssertions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#if !NETSTANDARD1_6
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -14,3 +15,4 @@ public class PartialViewResultAssertions : ViewResultBaseAssertions<PartialViewR
1415
public PartialViewResultAssertions(PartialViewResult viewResult) : base(viewResult) { }
1516
}
1617
}
18+
#endif

0 commit comments

Comments
 (0)