Skip to content

Commit 56b1ab6

Browse files
committed
Added unit tests for JsonTestBuilderExtensions.
1 parent dfdeab8 commit 56b1ab6

File tree

1 file changed

+137
-1
lines changed
  • test/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults.Test/BuildersTests/ActionResultsTests/JsonTests

1 file changed

+137
-1
lines changed

test/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults.Test/BuildersTests/ActionResultsTests/JsonTests/JsonTestBuilderTests.cs

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,53 @@ public void WithDefaultJsonSettingsShouldNotThrowExceptionWithDefaultJsonSetting
1919
.WithDefaultJsonSerializerSettings());
2020
}
2121

22+
[Fact]
23+
public void WithDefaultJsonSettingsShouldNotThrowExceptionAndPassAssertions()
24+
{
25+
MyController<MvcController>
26+
.Instance()
27+
.Calling(c => c.JsonAction())
28+
.ShouldReturn()
29+
.Json(json => json
30+
.WithDefaultJsonSerializerSettings()
31+
.AndAlso()
32+
.Passing(j =>
33+
{
34+
Assert.Null(j.SerializerSettings);
35+
}));
36+
}
37+
38+
[Fact]
39+
public void WithDefaultJsonSettingsShouldThrowExceptionWithNull()
40+
{
41+
Assert.Throws<InvocationAssertionException>(() =>
42+
{
43+
MyController<MvcController>
44+
.Instance()
45+
.Calling(c => c.JsonWithSpecificSettingsAction(null))
46+
.ShouldReturn()
47+
.Json(json => json
48+
.WithDefaultJsonSerializerSettings());
49+
});
50+
}
51+
52+
[Fact]
53+
public void WithDefaultJsonSettingsShouldThrowExceptionWithBuildInSerializationSettings()
54+
{
55+
var jsonSettings = new JsonSerializerSettings();
56+
57+
Test.AssertException<JsonResultAssertionException>(() =>
58+
{
59+
MyController<MvcController>
60+
.Instance()
61+
.Calling(c => c.JsonWithSpecificSettingsAction(jsonSettings))
62+
.ShouldReturn()
63+
.Json(json => json
64+
.WithDefaultJsonSerializerSettings());
65+
},
66+
"When calling JsonWithSpecificSettingsAction action in MvcController expected JSON result serializer settings to have DefaultContractResolver, but in fact found null.");
67+
}
68+
2269
[Fact]
2370
public void WithJsonSerializerSettingsShouldNotThrowExceptionWithSameJsonSettings()
2471
{
@@ -29,7 +76,96 @@ public void WithJsonSerializerSettingsShouldNotThrowExceptionWithSameJsonSetting
2976
.Json(json => json
3077
.WithJsonSerializerSettings(TestObjectFactory.GetJsonSerializerSettings()));
3178
}
32-
79+
80+
[Fact]
81+
public void WithJsonSerializerSettingsShouldNotThrowExceptionWithValidSettings()
82+
{
83+
var jsonSettings = TestObjectFactory.GetJsonSerializerSettings();
84+
85+
MyController<MvcController>
86+
.Instance()
87+
.Calling(c => c.JsonWithSpecificSettingsAction(jsonSettings))
88+
.ShouldReturn()
89+
.Json(json => json
90+
.WithJsonSerializerSettings(jsonSettings));
91+
}
92+
93+
[Fact]
94+
public void WithJsonSerializerSettingsShouldNotThrowExceptionWithBuiltInSettings()
95+
{
96+
var jsonSettings = new JsonSerializerSettings();
97+
98+
MyController<MvcController>
99+
.Instance()
100+
.Calling(c => c.JsonWithSpecificSettingsAction(jsonSettings))
101+
.ShouldReturn()
102+
.Json(json => json
103+
.WithJsonSerializerSettings(jsonSettings));
104+
}
105+
106+
[Fact]
107+
public void WithJsonSerializerSettingsShouldNotThrowExceptionWithActionSettings()
108+
{
109+
var jsonSettings = new JsonSerializerSettings
110+
{
111+
CheckAdditionalContent = true,
112+
NullValueHandling = NullValueHandling.Ignore
113+
};
114+
115+
MyController<MvcController>
116+
.Instance()
117+
.Calling(c => c.JsonWithSpecificSettingsAction(jsonSettings))
118+
.ShouldReturn()
119+
.Json(json => json
120+
.WithJsonSerializerSettings(settings =>
121+
{
122+
settings.WithAdditionalContentChecking(true);
123+
settings.WithNullValueHandling(NullValueHandling.Ignore);
124+
}));
125+
}
126+
127+
[Fact]
128+
public void WithJsonSerializerSettingsShouldThrowExceptionWithActionSettings()
129+
{
130+
var jsonSettings = new JsonSerializerSettings
131+
{
132+
CheckAdditionalContent = true,
133+
NullValueHandling = NullValueHandling.Ignore
134+
};
135+
136+
Test.AssertException<JsonResultAssertionException>(
137+
() =>
138+
{
139+
MyController<MvcController>
140+
.Instance()
141+
.Calling(c => c.JsonWithSpecificSettingsAction(jsonSettings))
142+
.ShouldReturn()
143+
.Json(json => json
144+
.WithJsonSerializerSettings(settings =>
145+
{
146+
settings.WithAdditionalContentChecking(false);
147+
settings.WithNullValueHandling(NullValueHandling.Ignore);
148+
}));
149+
},
150+
"When calling JsonWithSpecificSettingsAction action in MvcController expected JSON result serializer settings to have disabled checking for additional content, but in fact it was enabled.");
151+
}
152+
153+
[Fact]
154+
public void WithJsonSerializerSettingsShouldThrowExceptionWithNull()
155+
{
156+
var jsonSettings = TestObjectFactory.GetJsonSerializerSettings();
157+
158+
Assert.Throws<InvocationAssertionException>(() =>
159+
{
160+
MyController<MvcController>
161+
.Instance()
162+
.Calling(c => c.JsonWithSpecificSettingsAction(null))
163+
.ShouldReturn()
164+
.Json(json => json
165+
.WithJsonSerializerSettings(jsonSettings));
166+
});
167+
}
168+
33169
[Fact]
34170
public void WithJsonSerializerSettingsShouldThrowExceptionWithDifferentJsonSettings()
35171
{

0 commit comments

Comments
 (0)