forked from fluentassertions/fluentassertions.aspnetcore.mvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonResultAssertions.cs
126 lines (105 loc) · 4.79 KB
/
JsonResultAssertions.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
using FluentAssertions.Execution;
using FluentAssertions.Primitives;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Diagnostics;
namespace FluentAssertions.AspNetCore.Mvc
{
/// <summary>
/// Contains a number of methods to assert that a <see cref="JsonResult" /> is in the expected state.
/// </summary>
[DebuggerNonUserCode]
public class JsonResultAssertions : ObjectAssertions
{
#region Public Constructors
/// <summary>
/// Initializes a new instance of the <see cref="JsonResultAssertions" /> class.
/// </summary>
/// <param name="subject">The object to test assertion on</param>
public JsonResultAssertions(JsonResult subject) : base(subject)
{
}
#endregion
#region Public Properties
/// <summary>
/// The <see cref="JsonResult.SerializerSettings"/> on the tested subject.
/// </summary>
#if NETCOREAPP3_0_OR_GREATER
public object SerializerSettings => JsonResultSubject.SerializerSettings;
#else
public Newtonsoft.Json.JsonSerializerSettings SerializerSettings => JsonResultSubject.SerializerSettings;
#endif
/// <summary>
/// The <see cref="JsonResult.Value"/> on the tested subject.
/// </summary>
public object Value => JsonResultSubject.Value;
#endregion
#region Private Properties
private JsonResult JsonResultSubject => (JsonResult)Subject;
#endregion Private Properties
#region Public Methods
/// <summary>
/// Asserts that the content type is the expected content type.
/// </summary>
/// <param name="expectedContentType">The expected content type.</param>
/// <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 WithContentType(string expectedContentType, string reason = "",
params object[] reasonArgs)
{
var actualContentType = JsonResultSubject.ContentType;
Execute.Assertion
.ForCondition(string.Equals(expectedContentType, actualContentType, StringComparison.OrdinalIgnoreCase))
.BecauseOf(reason, reasonArgs)
.WithDefaultIdentifier("JsonResult.ContentType")
.FailWith(FailureMessages.CommonFailMessage, expectedContentType, actualContentType);
return this;
}
/// <summary>
/// Asserts that the status code is the expected status code.
/// </summary>
/// <param name="expectedStatusCode">The expected status code.</param>
/// <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 WithStatusCode(int? expectedStatusCode, string reason = "",
params object[] reasonArgs)
{
var actualStatusCode = JsonResultSubject.StatusCode;
Execute.Assertion
.ForCondition(expectedStatusCode == actualStatusCode)
.BecauseOf(reason, reasonArgs)
.WithDefaultIdentifier("JsonResult.StatusCode")
.FailWith(FailureMessages.CommonFailMessage, expectedStatusCode, actualStatusCode);
return this;
}
/// <summary>
/// Asserts the value is of the expected type.
/// </summary>
/// <typeparam name="TValue">The expected type.</typeparam>
/// <returns>The typed value.</returns>
public TValue ValueAs<TValue>()
{
var value = JsonResultSubject.Value;
if (value == null)
Execute.Assertion
.WithDefaultIdentifier("JsonResult.Value")
.FailWith(FailureMessages.CommonNullWasSuppliedFailMessage, typeof(TValue));
Execute.Assertion
.ForCondition(value is TValue)
.WithDefaultIdentifier("JsonResult.Value")
.FailWith(FailureMessages.CommonTypeFailMessage, typeof(TValue), value.GetType());
return (TValue)value;
}
#endregion
}
}