-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathActionResultAssertions.cs
242 lines (217 loc) · 9.66 KB
/
ActionResultAssertions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using FluentAssertions.Primitives;
using FluentAssertions.Execution;
using System;
#if NETSTANDARD1_6
using Microsoft.AspNetCore.Mvc;
#else
using System.Web.Mvc;
#endif
using System.Diagnostics;
namespace FluentAssertions.Mvc
{
/// <summary>
/// Contains a number of methods to assert that an <see cref="ActionResult"/> is in the expected state.
/// </summary>
[DebuggerNonUserCode]
public class ActionResultAssertions : ObjectAssertions
{
/// <summary>
/// Contains fixed values used in assertions
/// </summary>
public struct Constants
{
/// <summary>
/// A shared failure message
/// </summary>
public const string CommonFailMessage = "Expected ActionResult to be {0}{reason}, but found {1}";
}
/// <summary>
/// Initializes a new instance of the <see cref="T:ActionResultAssertions" /> class.
/// </summary>
public ActionResultAssertions (ActionResult subject) : base(subject)
{
Subject = subject;
}
#if NETSTANDARD1_6
public ActionResultAssertions (IActionResult subject): base(subject)
{
Subject = subject;
}
#endif
/// <summary>
/// Asserts that the subject is a <see cref="ContentResult"/>.
/// </summary>
public ContentResultAssertions BeContentResult()
{
return BeContentResult(string.Empty, null);
}
/// <summary>
/// Asserts that the subject is a <see cref="ContentResult"/>.
/// </summary>
/// <param name="reason">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="reasonArgs">
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
/// </param>
public ContentResultAssertions BeContentResult(string reason, params object[] reasonArgs)
{
Execute.Assertion
.BecauseOf(reason, reasonArgs)
.ForCondition(Subject is ContentResult)
.FailWith(Constants.CommonFailMessage, typeof(ContentResult).Name, Subject.GetType().Name);
return new ContentResultAssertions(Subject as ContentResult);
}
/// <summary>
/// Asserts that the subject is an <see cref="EmptyResult"/>.
/// </summary>
public EmptyResult BeEmptyResult()
{
return BeEmptyResult(string.Empty, null);
}
/// <summary>
/// Asserts that the subject is an <see cref="EmptyResult"/>.
/// </summary>
/// <param name="reason">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="reasonArgs">
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
/// </param>
public EmptyResult BeEmptyResult(string reason, params object[] reasonArgs)
{
Execute.Assertion
.BecauseOf(reason, reasonArgs)
.ForCondition(Subject is EmptyResult)
.FailWith(Constants.CommonFailMessage, typeof(EmptyResult).Name, Subject.GetType().Name);
return Subject as EmptyResult;
}
/// <summary>
/// Asserts that the subject is a <see cref="RedirectToRouteResult"/>.
/// </summary>
public RedirectToRouteAssertions BeRedirectToRouteResult()
{
return BeRedirectToRouteResult(string.Empty, null);
}
/// <summary>
/// Asserts that the subject is a <see cref="RedirectToRouteResult"/>.
/// </summary>
/// <param name="reason">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="reasonArgs">
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
/// </param>
public RedirectToRouteAssertions BeRedirectToRouteResult(string reason, params object[] reasonArgs)
{
Execute.Assertion
.BecauseOf(reason, reasonArgs)
.ForCondition(Subject is RedirectToRouteResult)
.FailWith(Constants.CommonFailMessage, typeof(RedirectToRouteResult).Name, Subject.GetType().Name);
return new RedirectToRouteAssertions(Subject as RedirectToRouteResult);
}
/// <summary>
/// Asserts that the subject is a <see cref="PartialViewResult"/>.
/// </summary>
public PartialViewResultAssertions BePartialViewResult()
{
return BePartialViewResult(string.Empty, null);
}
/// <summary>
/// Asserts that the subject is a <see cref="PartialViewResult"/>.
/// </summary>
/// <param name="reason">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="reasonArgs">
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
/// </param>
public PartialViewResultAssertions BePartialViewResult(string reason, params object[] reasonArgs)
{
Execute.Assertion
.BecauseOf(reason, reasonArgs)
.ForCondition(Subject is PartialViewResult)
.FailWith(Constants.CommonFailMessage, typeof(PartialViewResult).Name, Subject.GetType().Name);
return new PartialViewResultAssertions(Subject as PartialViewResult);
}
/// <summary>
/// Asserts that the subject is a <see cref="RedirectResult"/>.
/// </summary>
public RedirectResultAssertions BeRedirectResult()
{
return BeRedirectResult(string.Empty, null);
}
/// <summary>
/// Asserts that the subject is a <see cref="RedirectResult"/>.
/// </summary>
/// <param name="reason">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="reasonArgs">
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
/// </param>
public RedirectResultAssertions BeRedirectResult(string reason, params object[] reasonArgs)
{
Execute.Assertion
.BecauseOf(reason, reasonArgs)
.ForCondition(Subject is RedirectResult)
.FailWith(Constants.CommonFailMessage, "RedirectResult", Subject.GetType().Name);
return new RedirectResultAssertions(Subject as RedirectResult);
}
/// <summary>
/// Asserts that the subject is a <see cref="ViewResult"/>.
/// </summary>
public ViewResultAssertions BeViewResult()
{
return BeViewResult(string.Empty, null);
}
/// <summary>
/// Asserts that the subject is a <see cref="ViewResult"/>.
/// </summary>
/// <param name="reason">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="reasonArgs">
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
/// </param>
public ViewResultAssertions BeViewResult(string reason, params object[] reasonArgs)
{
Execute.Assertion
.BecauseOf(reason, reasonArgs)
.ForCondition (Subject is ViewResult)
.FailWith(Constants.CommonFailMessage, "ViewResult", Subject.GetType().Name);
return new ViewResultAssertions (Subject as ViewResult);
}
/// <summary>
/// Asserts that the subject is a <see cref="JsonResult"/>.
/// </summary>
public JsonResultAssertions BeJsonResult()
{
return BeJsonResult(string.Empty, null);
}
/// <summary>
/// Asserts that the subject is a <see cref="JsonResult"/>.
/// </summary>
/// <param name="reason">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="reasonArgs">
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
/// </param>
public JsonResultAssertions BeJsonResult(string reason, params object[] reasonArgs)
{
Execute.Assertion
.BecauseOf(reason, reasonArgs)
.ForCondition(Subject is JsonResult)
.FailWith(Constants.CommonFailMessage, typeof(JsonResult).Name, Subject.GetType().Name);
return new JsonResultAssertions(Subject as JsonResult);
}
}
}