forked from fluentassertions/fluentassertions.aspnetcore.mvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectResultAssertions_Tests.cs
304 lines (252 loc) · 10.3 KB
/
ObjectResultAssertions_Tests.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using System.Linq.Expressions;
using System.Net.Http.Headers;
using FluentAssertions.Mvc.Tests.Helpers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using Moq;
using Xunit;
namespace FluentAssertions.AspNetCore.Mvc.Tests
{
public class ObjectResultAssertions_Tests
{
private const string TestValue = "testValue";
private const string WrongValue = "wrongValue";
private readonly object TestObject = new { Value = "testValue" };
private readonly object GoodObject = new { Value = "testValue" };
private readonly object WrongObject = new { Value = "wrongValue" };
public const string Reason = FailureMessageHelper.Reason;
public readonly static object[] ReasonArgs = FailureMessageHelper.ReasonArgs;
[Fact]
public void Value_GivenObjectResult_ShouldHaveTheSameValue()
{
var result = new ObjectResult(TestValue);
result.Should().BeObjectResult().Value.Should().BeSameAs(TestValue);
}
[Fact]
public void ValueAs_GivenObjectResult_ShouldHaveTheSameValue()
{
var result = new ObjectResult(TestValue);
result.Should().BeObjectResult().ValueAs<string>().Should().BeSameAs(TestValue);
}
[Fact]
public void ValueAs_GivenWrongType_ShouldFail()
{
var result = new ObjectResult(TestValue);
string failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY(
"ObjectResult.Value", typeof(int), typeof(string));
Action a = () => result.Should().BeObjectResult().ValueAs<int>().Should().Be(2);
a.Should().Throw<Exception>()
.WithMessage(failureMessage);
}
[Fact]
public void ValueAs_Null_ShouldFail()
{
ActionResult result = new ObjectResult(null);
string failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundNull(
"ObjectResult.Value", typeof(object));
Action a = () => result.Should().BeObjectResult().ValueAs<object>();
a.Should().Throw<Exception>()
.WithMessage(failureMessage);
}
[Fact]
public void ContainsFormatter_GivenExpected_ShouldPass()
{
var formatterMock = new Mock<IOutputFormatter>();
var result = new ObjectResult(TestValue)
{
Formatters = { formatterMock.Object }
};
result.Should().BeObjectResult().ContainsFormatter(formatter => ReferenceEquals(formatter, formatterMock.Object));
}
[Fact]
public void ContainsFormatter_OnNullFormatters_ShouldFail()
{
var result = new ObjectResult(TestValue)
{
Formatters = null
};
Expression<Func<IOutputFormatter, bool>> expectation = formatter => formatter == null;
string failureMessage = FailureMessageHelper.ExpectedToContainItemButFoundNull(
"ObjectResult.Formatters",
expectation.Body);
Action a = () => result.Should().BeObjectResult().ContainsFormatter(expectation, Reason, ReasonArgs);
a.Should().Throw<Exception>()
.WithMessage(failureMessage);
}
[Fact]
public void ContainsFormatter_GivenUnexpected_ShouldFail()
{
var formatterMock = new Mock<IOutputFormatter>();
var result = new ObjectResult(TestValue)
{
Formatters = { formatterMock.Object }
};
string failureMessage = FailureMessageHelper.ExpectedToHaveItemMatching(
"ObjectResult.Formatters",
result.Formatters,
"False");
Action a = () => result.Should().BeObjectResult().ContainsFormatter(formatter => false, Reason, ReasonArgs);
a.Should().Throw<Exception>()
.WithMessage(failureMessage);
}
[Fact]
public void WithContentType_GivenExpected_ShouldPass()
{
var mediaType = "text/html";
var result = new ObjectResult(TestValue)
{
ContentTypes = { mediaType }
};
result.Should().BeObjectResult().WithContentType(mediaType);
}
#if NETCOREAPP2_1
// In NetCore3.1 ContentTypes cant be null.
[Fact]
public void WithContentType_OnNullMediaType_ShouldFail()
{
var expected = "text/json";
var result = new ObjectResult(TestValue)
{
ContentTypes = null
};
string failureMessage = FailureMessageHelper.ExpectedToContainItemButFoundNull(
"ObjectResult.ContentTypes",
expected);
Action a = () => result.Should().BeObjectResult().WithContentType(expected, Reason, ReasonArgs);
a.Should().Throw<Exception>()
.WithMessage(failureMessage);
}
#endif
[Fact]
public void WithContentType_GivenUnexpected_ShouldFail()
{
var mediaType = "text/html";
var expected = "text/json";
var result = new ObjectResult(TestValue)
{
ContentTypes = { mediaType }
};
string failureMessage = FailureMessageHelper.ExpectedToContainItem(
"ObjectResult.ContentTypes",
result.ContentTypes,
expected);
Action a = () => result.Should().BeObjectResult().WithContentType(expected, Reason, ReasonArgs);
a.Should().Throw<Exception>()
.WithMessage(failureMessage);
}
[Fact]
public void WithDeclaredType_GivenExpected_ShouldPass()
{
var expectedType = typeof(string);
var result = new ObjectResult(TestValue)
{
DeclaredType = expectedType
};
result.Should().BeObjectResult().WithDeclaredType(expectedType);
}
[Fact]
public void WithDeclaredType_GivenUnexpected_ShouldFail()
{
var expectedType = typeof(int);
var declaredType = typeof(string);
var result = new ObjectResult(TestValue)
{
DeclaredType = declaredType
};
string failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason(
"ObjectResult.DeclaredType",
expectedType,
declaredType);
Action a = () => result.Should().BeObjectResult().WithDeclaredType(expectedType, Reason, ReasonArgs);
a.Should().Throw<Exception>()
.WithMessage(failureMessage);
}
[Fact]
public void WithStatusCode_GivenExpected_ShouldPass()
{
var expectedStatusCode = 200;
var result = new ObjectResult(TestValue)
{
StatusCode = expectedStatusCode
};
result.Should().BeObjectResult().WithStatusCode(expectedStatusCode);
}
[Fact]
public void WithStatusCode_GivenUnexpected_ShouldFail()
{
var expectedStatusCode = 200;
var actualStatusCode = 500;
var result = new ObjectResult(TestValue)
{
StatusCode = actualStatusCode
};
string failureMessage = FailureMessageHelper.ExpectedContextToBeXButY(
"ObjectResult.StatusCode",
expectedStatusCode,
actualStatusCode);
Action a = () => result.Should().BeObjectResult().WithStatusCode(expectedStatusCode, Reason, ReasonArgs);
a.Should().Throw<Exception>()
.WithMessage(failureMessage);
}
[Fact]
public void WithValue_GivenExpected_ShouldPass()
{
var result = new ObjectResult(TestValue);
result.Should().BeObjectResult()
.WithValue(TestValue);
}
[Fact]
public void WithValue_GivenUnexpected_ShouldFail()
{
var result = new ObjectResult(WrongValue);
string failureMessage = FailureMessageHelper.ExpectedContextToBeXButY(
"ObjectResult.Value",
TestValue,
WrongValue);
Action a = () => result.Should().BeObjectResult().WithValue(TestValue, Reason, ReasonArgs);
a.Should().Throw<Exception>()
.WithMessage(failureMessage);
}
[Fact]
public void WithValueEquivalentTo_GivenExpected_ShouldPass()
{
var result = new ObjectResult(TestObject);
result.Should().BeObjectResult()
.WithValueEquivalentTo(GoodObject);
}
[Fact]
public void WithValueEquivalentTo_GivenUnexpected_ShouldFail()
{
var result = new ObjectResult(WrongObject);
string failureMessage = @"Expected member Value to be
""testValue"" with a length of 9 because it is 10, but
""wrongValue"" has a length of 10.
With configuration:
- Use declared types and members
- Compare enums by value
- Match member by name (or throw)
- Without automatic conversion.
- Be strict about the order of items in byte arrays";
Action a = () => result.Should().BeObjectResult().WithValueEquivalentTo(GoodObject, Reason, ReasonArgs);
a.Should().Throw<Exception>()
.WithMessage(failureMessage);
}
[Fact]
public void WithValueMatch_GivenExpected_ShouldPass()
{
var result = new ObjectResult(TestValue);
result.Should().BeObjectResult()
.WithValueMatch<string>(value => value == TestValue);
}
[Fact]
public void WithValueMatch_GivenUnexpected_ShouldFail()
{
var result = new ObjectResult(WrongValue);
string failureMessage = "Expected ObjectResult.Value to match (value == \"testValue\") because it is 10, but found \"wrongValue\".";
Action a = () => result.Should().BeObjectResult().WithValueMatch<string>(value => value == TestValue, Reason, ReasonArgs);
a.Should().Throw<Exception>()
.WithMessage(failureMessage);
}
}
}