Skip to content

Commit 779f88e

Browse files
committed
Added unit tests for BaseTestBuilderWithViewResultExtensions.
1 parent 4252131 commit 779f88e

File tree

1 file changed

+216
-3
lines changed
  • test/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults.Test/BuildersTests/ActionsTests/ShouldReturnTests

1 file changed

+216
-3
lines changed

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

Lines changed: 216 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.ActionsTests.ShouldReturnTests
22
{
33
using Exceptions;
4+
using Microsoft.AspNetCore.Mvc;
5+
using MyTested.AspNetCore.Mvc.Test.Setups.Models;
46
using Setups;
57
using Setups.Controllers;
8+
using System.Collections.Generic;
69
using Xunit;
710

811
public class ShouldReturnViewTests
@@ -17,7 +20,47 @@ public void ShouldReturnViewWithNameShouldNotThrowExceptionWithCorrectName()
1720
.View(view => view
1821
.WithName("Index"));
1922
}
20-
23+
24+
[Fact]
25+
public void ShouldReturnViewWithNameShouldNotThrowExceptionAndPassAssertions()
26+
{
27+
MyController<MvcController>
28+
.Instance()
29+
.Calling(c => c.IndexView())
30+
.ShouldReturn()
31+
.View(view => view
32+
.Passing((v) =>
33+
{
34+
Assert.False(string.IsNullOrEmpty(v.ViewName));
35+
Assert.Equal("Index", v.ViewName);
36+
Assert.True(typeof(ViewResult) == v.GetType());
37+
}));
38+
}
39+
40+
[Fact]
41+
public void ShouldReturnViewShouldNotThrowExceptionWithCorrectViewName()
42+
{
43+
MyController<MvcController>
44+
.Instance()
45+
.Calling(c => c.ViewResultByName())
46+
.ShouldReturn()
47+
.View(view => view
48+
.WithName("TestView"));
49+
}
50+
51+
[Fact]
52+
public void ShouldReturnViewWithNameShouldNotThrowExceptionWithCorrectViewResultType()
53+
{
54+
MyController<MvcController>
55+
.Instance()
56+
.Calling(c => c.IndexView())
57+
.ShouldReturn()
58+
.View(view => view
59+
.WithName("Index")
60+
.AndAlso()
61+
.Passing(v => v.GetType() == typeof(ViewResult)));
62+
}
63+
2164
[Fact]
2265
public void ShouldReturnViewShouldThrowExceptionIfActionResultIsViewResultWithDifferentName()
2366
{
@@ -33,7 +76,39 @@ public void ShouldReturnViewShouldThrowExceptionIfActionResultIsViewResultWithDi
3376
},
3477
"When calling IndexView action in MvcController expected view result to be 'Incorrect', but instead received 'Index'.");
3578
}
36-
79+
80+
[Fact]
81+
public void ShouldReturnViewShouldThrowExceptionWithNullViewName()
82+
{
83+
Test.AssertException<ViewResultAssertionException>(
84+
() =>
85+
{
86+
MyController<MvcController>
87+
.Instance()
88+
.Calling(c => c.IndexView())
89+
.ShouldReturn()
90+
.View(view => view
91+
.WithName(null));
92+
},
93+
"When calling IndexView action in MvcController expected view result to be the default one, but instead received 'Index'.");
94+
}
95+
96+
[Fact]
97+
public void ShouldReturnViewShouldThrowExceptionWithIncorrectViewResultAndName()
98+
{
99+
Test.AssertException<InvocationResultAssertionException>(
100+
() =>
101+
{
102+
MyController<MvcController>
103+
.Instance()
104+
.Calling(c => c.BadRequestAction())
105+
.ShouldReturn()
106+
.View(view => view
107+
.WithName("TestView"));
108+
},
109+
"When calling BadRequestAction action in MvcController expected result to be ViewResult, but instead received BadRequestResult.");
110+
}
111+
37112
[Fact]
38113
public void ShouldReturnPartialViewWithNameShouldNotThrowExceptionWithCorrectName()
39114
{
@@ -44,7 +119,23 @@ public void ShouldReturnPartialViewWithNameShouldNotThrowExceptionWithCorrectNam
44119
.PartialView(view => view
45120
.WithName("_IndexPartial"));
46121
}
47-
122+
123+
[Fact]
124+
public void ShouldReturnPartialViewWithNameShouldNotThrowExceptionAndPassAssertions()
125+
{
126+
MyController<MvcController>
127+
.Instance()
128+
.Calling(c => c.IndexPartialView())
129+
.ShouldReturn()
130+
.PartialView(view => view
131+
.Passing(v =>
132+
{
133+
Assert.False(string.IsNullOrEmpty(v.ViewName));
134+
Assert.Equal("_IndexPartial", v.ViewName);
135+
Assert.True(typeof(PartialViewResult) == v.GetType());
136+
}));
137+
}
138+
48139
[Fact]
49140
public void ShouldReturnPartialViewShouldThrowExceptionIfActionResultIsViewResultWithDifferentName()
50141
{
@@ -60,5 +151,127 @@ public void ShouldReturnPartialViewShouldThrowExceptionIfActionResultIsViewResul
60151
},
61152
"When calling IndexPartialView action in MvcController expected partial view result to be 'Incorrect', but instead received '_IndexPartial'.");
62153
}
154+
155+
[Fact]
156+
public void ShouldReturnPartialViewShouldThrowExceptionWithNullViewName()
157+
{
158+
Test.AssertException<ViewResultAssertionException>(
159+
() =>
160+
{
161+
MyController<MvcController>
162+
.Instance()
163+
.Calling(c => c.IndexPartialView())
164+
.ShouldReturn()
165+
.PartialView(view => view
166+
.WithName(null));
167+
},
168+
"When calling IndexPartialView action in MvcController expected partial view result to be the default one, but instead received '_IndexPartial'.");
169+
}
170+
171+
[Fact]
172+
public void ShouldReturnParrtialViewShouldThrowExceptionWithIncorrectViewResult()
173+
{
174+
Test.AssertException<InvocationResultAssertionException>(
175+
() =>
176+
{
177+
MyController<MvcController>
178+
.Instance()
179+
.Calling(c => c.BadRequestAction())
180+
.ShouldReturn()
181+
.PartialView(view => view
182+
.WithName("_IndexPartial"));
183+
},
184+
"When calling BadRequestAction action in MvcController expected result to be PartialViewResult, but instead received BadRequestResult.");
185+
}
186+
187+
[Fact]
188+
public void ShouldReturnViewWithDefaultNameShouldNotThrowException()
189+
{
190+
MyController<MvcController>
191+
.Instance()
192+
.Calling(c => c.DefaultView())
193+
.ShouldReturn()
194+
.View(view => view
195+
.WithDefaultName());
196+
}
197+
198+
[Fact]
199+
public void ShouldReturnViewWithDefaultNameShouldNotThrowExceptionAndPassAssertions()
200+
{
201+
MyController<MvcController>
202+
.Instance()
203+
.Calling(c => c.DefaultView())
204+
.ShouldReturn()
205+
.View(view => view
206+
.Passing(v =>
207+
{
208+
Assert.True(string.IsNullOrEmpty(v.ViewName));
209+
Assert.True(v.GetType() == typeof(ViewResult));
210+
}));
211+
}
212+
213+
[Fact]
214+
public void ShouldReturnViewWithDefaultNameAndViewModelShouldNotThrowException()
215+
{
216+
MyController<MvcController>
217+
.Instance()
218+
.Calling(c => c.DefaultViewWithModel())
219+
.ShouldReturn()
220+
.View(view => view
221+
.WithDefaultName()
222+
.AndAlso()
223+
.WithModelOfType<ICollection<ResponseModel>>());
224+
}
225+
226+
[Fact]
227+
public void ShouldReturnViewWithDefaultNameAndCustomViewResultTypeShouldNotThrowExceptionWithCorrectType()
228+
{
229+
MyController<MvcController>
230+
.Instance()
231+
.Calling(c => c.CustomViewResult())
232+
.ShouldReturn()
233+
.View(view => view
234+
.WithDefaultName()
235+
.Passing(v => v.GetType() == typeof(ViewResult)));
236+
}
237+
238+
[Fact]
239+
public void ShouldReturnPartialViewWithDefaultNameShouldNotThrowException()
240+
{
241+
MyController<MvcController>
242+
.Instance()
243+
.Calling(c => c.DefaultPartialView())
244+
.ShouldReturn()
245+
.PartialView(view => view
246+
.WithDefaultName());
247+
}
248+
249+
[Fact]
250+
public void ShouldReturnPartialViewWithDefaultNameShouldNotThrowExceptionAndPassAssertions()
251+
{
252+
MyController<MvcController>
253+
.Instance()
254+
.Calling(c => c.DefaultPartialView())
255+
.ShouldReturn()
256+
.PartialView(view => view
257+
.Passing(v =>
258+
{
259+
Assert.True(string.IsNullOrEmpty(v.ViewName));
260+
Assert.True(v.GetType() == typeof(PartialViewResult));
261+
}));
262+
}
263+
264+
[Fact]
265+
public void ShouldReturnPartialViewWithDefaultNameAndViewModelShouldNotThrowException()
266+
{
267+
MyController<MvcController>
268+
.Instance()
269+
.Calling(c => c.DefaultPartialViewWithModel())
270+
.ShouldReturn()
271+
.PartialView(view => view
272+
.WithDefaultName()
273+
.AndAlso()
274+
.WithModelOfType<ICollection<ResponseModel>>());
275+
}
63276
}
64277
}

0 commit comments

Comments
 (0)