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+ }
0 commit comments