Skip to content

Commit e01eca3

Browse files
authored
Merge pull request #313 - Adding tests for JSON test builder, views and view components
Adding couple of tests for JSON test builder, view and view component
2 parents 1716f32 + ca3a1fb commit e01eca3

File tree

8 files changed

+403
-43
lines changed

8 files changed

+403
-43
lines changed

src/MyTested.AspNetCore.Mvc.Controllers.Views/Builders/ActionResults/View/BaseTestBuilderWithViewFeatureResult.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ public override object GetActualModel()
5252
{
5353
return (this.ActionResult as ViewResult).Model;
5454
}
55+
else if (this.ActionResult is PartialViewResult)
56+
{
57+
return (this.ActionResult as PartialViewResult)?.ViewData?.Model;
58+
}
5559

56-
return (this.ActionResult as PartialViewResult)?.ViewData?.Model;
60+
return (this.ActionResult as ViewComponentResult)?.ViewData?.Model;
5761
}
5862

5963
public override void ValidateNoModel()

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void WithResponseModelOfTypeShouldWorkCorrectlyWithJson()
2121
.Json(json => json
2222
.WithModelOfType<ICollection<ResponseModel>>());
2323
}
24-
24+
2525
[Fact]
2626
public void WithHttpStatusCodeShouldNotThrowExceptionWithCorrectStatusCode()
2727
{
@@ -48,7 +48,7 @@ public void WithHttpStatusCodeShouldThrowExceptionWithIncorrectStatusCode()
4848
},
4949
"When calling JsonWithStatusCodeAction action in MvcController expected JSON result to have 500 (InternalServerError) status code, but instead received 200 (OK).");
5050
}
51-
51+
5252
[Fact]
5353
public void WithContentTypeShouldNotThrowExceptionWithCorrectContentType()
5454
{
@@ -102,7 +102,7 @@ public void WithContentTypeAsMediaTypeHeaderValueShouldThrowExceptionWithNullCon
102102
},
103103
"When calling JsonWithStatusCodeAction action in MvcController expected JSON result ContentType to be null, but instead received 'application/xml'.");
104104
}
105-
105+
106106
[Fact]
107107
public void AndAlsoShouldWorkCorrectly()
108108
{
@@ -131,5 +131,15 @@ public void AndProvideTheActionResultShouldWorkCorrectly()
131131
Assert.IsAssignableFrom<JsonResult>(actionResult);
132132
});
133133
}
134+
135+
[Fact]
136+
public void WithNullJsonShouldReturnNoModel()
137+
{
138+
MyController<MvcController>
139+
.Instance()
140+
.Calling(c => c.NullJsonAction())
141+
.ShouldReturn()
142+
.Json(json => json.WithNoModel());
143+
}
134144
}
135-
}
145+
}

test/MyTested.AspNetCore.Mvc.Controllers.Views.Test/BuildersTests/ActionResultsTests/ViewTests/ViewComponentTestBuilderTests.cs

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.ActionResultsTests.ViewTests
22
{
3+
using System.Collections.Generic;
34
using System.Net;
45
using Exceptions;
56
using Microsoft.AspNetCore.Mvc;
67
using Microsoft.Net.Http.Headers;
8+
using MyTested.AspNetCore.Mvc.Test.Setups.Models;
79
using Setups;
810
using Setups.Controllers;
911
using Xunit;
@@ -139,7 +141,7 @@ public void WithMediaTypeShouldThrowExceptionWithMediaTypeHeaderValueAndNullActu
139141
},
140142
"When calling ViewComponentResultByName action in MvcController expected view component result ContentType to be 'application/json', but instead received null.");
141143
}
142-
144+
143145
[Fact]
144146
public void AndAlsoShouldWorkCorrectly()
145147
{
@@ -168,5 +170,85 @@ public void AndProvideTheActionResultShouldWorkCorrectly()
168170
Assert.IsAssignableFrom<ViewComponentResult>(actionResult);
169171
});
170172
}
173+
174+
[Fact]
175+
public void WithNoModelShouldNotThrowExceptionWhenNoModelIsReturned()
176+
{
177+
MyController<MvcController>
178+
.Instance()
179+
.Calling(c => c.ViewComponentResultByType())
180+
.ShouldReturn()
181+
.ViewComponent(viewComponent =>
182+
viewComponent.WithNoModel());
183+
}
184+
185+
[Fact]
186+
public void WithModelOfTypeShouldNotThrowExceptionWithCorrectTypeForViewComponents()
187+
{
188+
MyController<MvcController>
189+
.Instance()
190+
.Calling(c => c.CustomViewComponentResultWithViewData())
191+
.ShouldReturn()
192+
.ViewComponent(viewComponent =>
193+
viewComponent.WithModelOfType<List<ResponseModel>>());
194+
}
195+
196+
[Fact]
197+
public void WithModelShouldNotThrowExceptionWithCorrectModelForViewComponent()
198+
{
199+
MyController<MvcController>
200+
.Instance()
201+
.Calling(c => c.CustomViewComponentResultWithViewData())
202+
.ShouldReturn()
203+
.ViewComponent(viewComponent => viewComponent
204+
.WithModel(TestObjectFactory.GetListOfResponseModels()));
205+
}
206+
207+
[Fact]
208+
public void WithModelShouldExceptionWithCorrectModelForViewComponentWhenSuchDiffers()
209+
{
210+
Test.AssertException<ResponseModelAssertionException>(
211+
() =>
212+
{
213+
MyController<MvcController>
214+
.Instance()
215+
.Calling(c => c.CustomViewComponentResultWithViewData())
216+
.ShouldReturn()
217+
.ViewComponent(viewComponent => viewComponent
218+
.WithModel(new object()));
219+
},
220+
"When calling CustomViewComponentResultWithViewData action in MvcController expected response model Object to be the given model, but in fact it was a different one.");
221+
}
222+
223+
[Fact]
224+
public void AndProvideTheActionResultShouldWorkCorrectlyForViewComponentModel()
225+
{
226+
MyController<MvcController>
227+
.Instance()
228+
.Calling(c => c.CustomViewComponentResultWithViewData())
229+
.ShouldReturn()
230+
.ViewComponent()
231+
.ShouldPassForThe<IActionResult>(actionResult =>
232+
{
233+
Assert.NotNull(actionResult);
234+
Assert.NotNull((actionResult as ViewComponentResult).Model);
235+
});
236+
}
237+
238+
[Fact]
239+
public void WithNoModelShouldExceptionWithCorrectModelForViewComponentWhenSuchExist()
240+
{
241+
Test.AssertException<ResponseModelAssertionException>(
242+
() =>
243+
{
244+
MyController<MvcController>
245+
.Instance()
246+
.Calling(c => c.CustomViewComponentResultWithViewData())
247+
.ShouldReturn()
248+
.ViewComponent(viewComponent => viewComponent
249+
.WithNoModel());
250+
},
251+
"When calling CustomViewComponentResultWithViewData action in MvcController expected to not have a view model but in fact such was found.");
252+
}
171253
}
172254
}

test/MyTested.AspNetCore.Mvc.Controllers.Views.Test/BuildersTests/ActionResultsTests/ViewTests/ViewTestBuilderTests.cs

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public void WithMediaTypeShouldThrowExceptionWithMediaTypeHeaderValueAndNullActu
214214
},
215215
"When calling DefaultView action in MvcController expected view result ContentType to be 'application/json', but instead received null.");
216216
}
217-
217+
218218
[Fact]
219219
public void AndAlsoShouldWorkCorrectly()
220220
{
@@ -227,7 +227,25 @@ public void AndAlsoShouldWorkCorrectly()
227227
.AndAlso()
228228
.WithStatusCode(500));
229229
}
230-
230+
231+
[Fact]
232+
public void AndAlsoShouldWorkCorrectlyWhenDifferInStatusCode()
233+
{
234+
Test.AssertException<ViewResultAssertionException>(
235+
() =>
236+
{
237+
MyController<MvcController>
238+
.Instance()
239+
.Calling(c => c.CustomViewResult())
240+
.ShouldReturn()
241+
.View(view => view
242+
.WithContentType(ContentType.ApplicationXml)
243+
.AndAlso()
244+
.WithStatusCode(200));
245+
},
246+
"When calling CustomViewResult action in MvcController expected view result to have 200 (OK) status code, but instead received 500 (InternalServerError).");
247+
}
248+
231249
[Fact]
232250
public void WithModelShouldNotThrowExceptionWithCorrectModelForPartials()
233251
{
@@ -312,6 +330,17 @@ public void WithStatusCodeAsIntShouldNotThrowExceptionWhenActionReturnsCorrectSt
312330
.WithStatusCode(500));
313331
}
314332

333+
[Fact]
334+
public void WithNoModelShouldNotThrowExceptionWhenModelIsNotProvided()
335+
{
336+
MyController<MvcController>
337+
.Instance()
338+
.Calling(c => c.CustomPartialViewResult())
339+
.ShouldReturn()
340+
.PartialView(partialView => partialView
341+
.WithNoModel());
342+
}
343+
315344
[Fact]
316345
public void WithStatusCodeShouldNotThrowExceptionWhenActionReturnsCorrectStatusCodeForPartials()
317346
{
@@ -457,6 +486,24 @@ public void AndAlsoShouldWorkCorrectlyForPartials()
457486
.WithStatusCode(500));
458487
}
459488

489+
[Fact]
490+
public void AndAlsoShouldWorkCorrectlyForPartialsWhenDifferInStatusCode()
491+
{
492+
Test.AssertException<ViewResultAssertionException>(
493+
() =>
494+
{
495+
MyController<MvcController>
496+
.Instance()
497+
.Calling(c => c.CustomPartialViewResult())
498+
.ShouldReturn()
499+
.PartialView(view => view
500+
.WithContentType(ContentType.ApplicationXml)
501+
.AndAlso()
502+
.WithStatusCode(200));
503+
},
504+
"When calling CustomPartialViewResult action in MvcController expected partial view result to have 200 (OK) status code, but instead received 500 (InternalServerError).");
505+
}
506+
460507
[Fact]
461508
public void AndProvideTheActionResultShouldWorkCorrectly()
462509
{
@@ -472,7 +519,7 @@ public void AndProvideTheActionResultShouldWorkCorrectly()
472519
Assert.IsAssignableFrom<ViewResult>(actionResult);
473520
});
474521
}
475-
522+
476523
[Fact]
477524
public void AndProvideTheActionResultShouldWorkCorrectlyWithPartial()
478525
{
@@ -488,5 +535,74 @@ public void AndProvideTheActionResultShouldWorkCorrectlyWithPartial()
488535
Assert.IsAssignableFrom<PartialViewResult>(actionResult);
489536
});
490537
}
538+
539+
[Fact]
540+
public void WithModelOfTypeShouldNotThrowExceptionWithCorrectTypeForPartialView()
541+
{
542+
MyController<MvcController>
543+
.Instance()
544+
.Calling(c => c.CustomPartialViewResultWithViewData())
545+
.ShouldReturn()
546+
.PartialView(partialView =>
547+
partialView.WithModelOfType<List<ResponseModel>>());
548+
}
549+
550+
[Fact]
551+
public void WithModelShouldNotThrowExceptionWithCorrectModelForPartialView()
552+
{
553+
MyController<MvcController>
554+
.Instance()
555+
.Calling(c => c.CustomPartialViewResultWithViewData())
556+
.ShouldReturn()
557+
.PartialView(partialView => partialView
558+
.WithModel(TestObjectFactory.GetListOfResponseModels()));
559+
}
560+
561+
[Fact]
562+
public void WithModelShouldExceptionWithCorrectModelForPartialViewWhenSuchDiffers()
563+
{
564+
Test.AssertException<ResponseModelAssertionException>(
565+
() =>
566+
{
567+
MyController<MvcController>
568+
.Instance()
569+
.Calling(c => c.CustomPartialViewResultWithViewData())
570+
.ShouldReturn()
571+
.PartialView(partialView => partialView
572+
.WithModel(new object()));
573+
},
574+
"When calling CustomPartialViewResultWithViewData action in MvcController expected response model Object to be the given model, but in fact it was a different one.");
575+
}
576+
577+
[Fact]
578+
public void AndProvideTheActionResultShouldWorkCorrectlyForPartialViewModel()
579+
{
580+
MyController<MvcController>
581+
.Instance()
582+
.Calling(c => c.CustomPartialViewResultWithViewData())
583+
.ShouldReturn()
584+
.PartialView()
585+
.ShouldPassForThe<IActionResult>(actionResult =>
586+
{
587+
Assert.NotNull(actionResult);
588+
Assert.NotNull((actionResult as PartialViewResult).Model);
589+
});
590+
}
591+
592+
[Fact]
593+
public void WithNoModelShouldExceptionWithCorrectModelForPartialViewWhenSuchExist()
594+
{
595+
Test.AssertException<ResponseModelAssertionException>(
596+
() =>
597+
{
598+
MyController<MvcController>
599+
.Instance()
600+
.Calling(c => c.CustomPartialViewResultWithViewData())
601+
.ShouldReturn()
602+
.PartialView(partialView => partialView
603+
.WithNoModel());
604+
},
605+
"When calling CustomPartialViewResultWithViewData action in MvcController expected to not have a view model but in fact such was found.");
606+
}
491607
}
492608
}

test/MyTested.AspNetCore.Mvc.Controllers.Views.Test/BuildersTests/ActionsTests/ShouldReturnTests/ShouldReturnJsonTests.cs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Setups;
55
using Setups.Controllers;
66
using Xunit;
7-
7+
88
public class ShouldReturnJsonTests
99
{
1010
[Fact]
@@ -28,8 +28,58 @@ public void ShouldReturnJsonShouldThrowExceptionIfResultIsNotJson()
2828
.Calling(c => c.BadRequestAction())
2929
.ShouldReturn()
3030
.Json();
31-
},
31+
},
3232
"When calling BadRequestAction action in MvcController expected result to be JsonResult, but instead received BadRequestResult.");
3333
}
34+
35+
[Fact]
36+
public void ShouldReturnJsonShouldNotThrowExceptionIfResultIsEmptyJson()
37+
{
38+
MyController<MvcController>
39+
.Instance()
40+
.Calling(c => c.EmptyJsonAction())
41+
.ShouldReturn()
42+
.Json();
43+
}
44+
45+
[Fact]
46+
public void ShouldReturnJsonShouldNotThrowExceptionIfResultIsNullJson()
47+
{
48+
MyController<MvcController>
49+
.Instance()
50+
.Calling(c => c.NullJsonAction())
51+
.ShouldReturn()
52+
.Json();
53+
}
54+
55+
[Fact]
56+
public void ShouldReturnJsonShouldThrowExceptionIfResultIsNullAction()
57+
{
58+
Test.AssertException<InvocationResultAssertionException>(
59+
() =>
60+
{
61+
MyController<MvcController>
62+
.Instance()
63+
.Calling(c => c.NullAction())
64+
.ShouldReturn()
65+
.Json();
66+
},
67+
"When calling NullAction action in MvcController expected result to be JsonResult, but instead received null.");
68+
}
69+
70+
[Fact]
71+
public void ShouldReturnJsonShouldThrowExceptionIfResultIsContentAction()
72+
{
73+
Test.AssertException<InvocationResultAssertionException>(
74+
() =>
75+
{
76+
MyController<MvcController>
77+
.Instance()
78+
.Calling(c => c.ContentAction())
79+
.ShouldReturn()
80+
.Json();
81+
},
82+
"When calling ContentAction action in MvcController expected result to be JsonResult, but instead received ContentResult.");
83+
}
3484
}
3585
}

0 commit comments

Comments
 (0)