Skip to content

Commit 67a7d91

Browse files
committed
Added unit tests for ViewTestBuilder.
1 parent 779f88e commit 67a7d91

File tree

1 file changed

+178
-0
lines changed
  • test/MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults.Test/BuildersTests/ActionResultsTests/ViewTests

1 file changed

+178
-0
lines changed

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

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,51 @@ public void WithViewEngineShouldNotThrowExceptionWithValidViewEngine()
2222
.WithViewEngine(viewEngine));
2323
}
2424

25+
[Fact]
26+
public void WithViewEngineShouldNotThrowExceptionWithBuiltInView()
27+
{
28+
MyController<MvcController>
29+
.Instance()
30+
.Calling(c => c.View())
31+
.ShouldReturn()
32+
.View(view => view
33+
.WithViewEngine(null));
34+
}
35+
36+
[Fact]
37+
public void WithViewEngineShouldNotThrowExceptionWithValidViewEngineAndPassAssertions()
38+
{
39+
var viewEngine = TestObjectFactory.GetViewEngine();
40+
41+
MyController<MvcController>
42+
.Instance()
43+
.Calling(c => c.ViewWithViewEngine(viewEngine))
44+
.ShouldReturn()
45+
.View(view => view
46+
.WithViewEngine(viewEngine)
47+
.AndAlso()
48+
.Passing(v =>
49+
{
50+
Assert.NotNull(v.ViewEngine);
51+
Assert.IsAssignableFrom<IViewEngine>(v.ViewEngine);
52+
Assert.True(typeof(CustomViewEngine) == v.ViewEngine.GetType());
53+
}));
54+
}
55+
56+
[Fact]
57+
public void WithViewEngineShouldNotThrowExceptionWithBuiltInViewAndPassAssertions()
58+
{
59+
MyController<MvcController>
60+
.Instance()
61+
.Calling(c => c.View())
62+
.ShouldReturn()
63+
.View(view => view
64+
.Passing(v =>
65+
{
66+
Assert.Null(v.ViewEngine);
67+
}));
68+
}
69+
2570
[Fact]
2671
public void WithViewEngineShouldNotThrowExceptionWithNullViewEngine()
2772
{
@@ -50,6 +95,25 @@ public void WithViewEngineShouldThrowExceptionWithInvalidViewEngine()
5095
"When calling ViewWithViewEngine action in MvcController expected view result engine to be the same as the provided one, but instead received different result.");
5196
}
5297

98+
[Fact]
99+
public void WithViewEngineShouldThrowExceptionWithIncorrectValue()
100+
{
101+
var viewEngine = TestObjectFactory.GetViewEngine();
102+
103+
Test.AssertException<ViewResultAssertionException>(
104+
() =>
105+
{
106+
MyController<MvcController>
107+
.Instance()
108+
.WithoutValidation()
109+
.Calling(c => c.ViewWithViewEngine(viewEngine))
110+
.ShouldReturn()
111+
.View(view => view
112+
.WithViewEngine(null));
113+
},
114+
"When calling ViewWithViewEngine action in MvcController expected view result engine to be the same as the provided one, but instead received different result.");
115+
}
116+
53117
[Fact]
54118
public void WithViewEngineOfTypeShouldNotThrowExceptionWithValidViewEngine()
55119
{
@@ -61,6 +125,47 @@ public void WithViewEngineOfTypeShouldNotThrowExceptionWithValidViewEngine()
61125
.WithViewEngineOfType<CustomViewEngine>());
62126
}
63127

128+
[Fact]
129+
public void WithViewEngineOfTypeShouldNotThrowExceptionWithValidViewEngineAndCustomViewResult()
130+
{
131+
MyController<MvcController>
132+
.Instance()
133+
.Calling(c => c.CustomViewResult())
134+
.ShouldReturn()
135+
.View(view => view
136+
.WithViewEngineOfType<CustomViewEngine>());
137+
}
138+
139+
[Fact]
140+
public void WithViewEngineOfTypeShouldThrowExceptionWithInvalidTypeAndCustomViewResult()
141+
{
142+
Test.AssertException<ViewResultAssertionException>(() =>
143+
{
144+
MyController<MvcController>
145+
.Instance()
146+
.Calling(c => c.CustomViewResult())
147+
.ShouldReturn()
148+
.View(view => view
149+
.WithViewEngineOfType<IViewEngine>());
150+
},
151+
"When calling CustomViewResult action in MvcController expected view result engine to be of IViewEngine type, but instead received CustomViewEngine.");
152+
}
153+
154+
[Fact]
155+
public void WithViewEngineOfTypeShouldThrowExceptionWithDifferentTypes()
156+
{
157+
Test.AssertException<ViewResultAssertionException>(() =>
158+
{
159+
MyController<MvcController>
160+
.Instance()
161+
.Calling(c => c.CustomViewResult())
162+
.ShouldReturn()
163+
.View(view => view
164+
.WithViewEngineOfType<CompositeViewEngine>());
165+
},
166+
"When calling CustomViewResult action in MvcController expected view result engine to be of CompositeViewEngine type, but instead received CustomViewEngine.");
167+
}
168+
64169
[Fact]
65170
public void WithViewEngineOfTypeShouldThrowExceptionWithInvalidViewEngine()
66171
{
@@ -107,6 +212,41 @@ public void WithViewEngineShouldNotThrowExceptionWithValidViewEngineForPartials(
107212
.WithViewEngine(viewEngine));
108213
}
109214

215+
[Fact]
216+
public void WithViewEngineShouldNotThrowExceptionWithValidViewEngineForPartialsAndPassAssertions()
217+
{
218+
var viewEngine = TestObjectFactory.GetViewEngine();
219+
220+
MyController<MvcController>
221+
.Instance()
222+
.WithoutValidation()
223+
.Calling(c => c.PartialViewWithViewEngine(viewEngine))
224+
.ShouldReturn()
225+
.PartialView(partialView => partialView
226+
.WithViewEngine(viewEngine)
227+
.AndAlso()
228+
.Passing(pv =>
229+
{
230+
Assert.NotNull(pv.ViewEngine);
231+
Assert.IsAssignableFrom<IViewEngine>(pv.ViewEngine);
232+
Assert.True(typeof(CustomViewEngine) == pv.ViewEngine.GetType());
233+
}));
234+
}
235+
236+
[Fact]
237+
public void WithViewEngineShouldNotThrowExceptionWithValidViewEngineForBuiltInPartials()
238+
{
239+
var viewEngine = TestObjectFactory.GetViewEngine();
240+
241+
MyController<MvcController>
242+
.Instance()
243+
.WithoutValidation()
244+
.Calling(c => c.PartialView())
245+
.ShouldReturn()
246+
.PartialView(partialView => partialView
247+
.WithViewEngine(null));
248+
}
249+
110250
[Fact]
111251
public void WithViewEngineShouldNotThrowExceptionWithNullViewEngineForPartials()
112252
{
@@ -119,6 +259,25 @@ public void WithViewEngineShouldNotThrowExceptionWithNullViewEngineForPartials()
119259
.WithViewEngine(null));
120260
}
121261

262+
[Fact]
263+
public void WithViewEngineShouldThrowExceptionWithCustomViewEngineForPartials()
264+
{
265+
var viewEngine = TestObjectFactory.GetViewEngine();
266+
267+
Test.AssertException<ViewResultAssertionException>(
268+
() =>
269+
{
270+
MyController<MvcController>
271+
.Instance()
272+
.WithoutValidation()
273+
.Calling(c => c.DefaultPartialView())
274+
.ShouldReturn()
275+
.PartialView(partialView => partialView
276+
.WithViewEngine(viewEngine));
277+
},
278+
"When calling DefaultPartialView action in MvcController expected partial view result engine to be the same as the provided one, but instead received different result.");
279+
}
280+
122281
[Fact]
123282
public void WithViewEngineShouldThrowExceptionWithInvalidViewEngineForPartials()
124283
{
@@ -136,6 +295,25 @@ public void WithViewEngineShouldThrowExceptionWithInvalidViewEngineForPartials()
136295
"When calling PartialViewWithViewEngine action in MvcController expected partial view result engine to be the same as the provided one, but instead received different result.");
137296
}
138297

298+
[Fact]
299+
public void WithViewEngineForPartialsShouldThrowExceptionWithIncorrectValue()
300+
{
301+
var viewEngine = TestObjectFactory.GetViewEngine();
302+
303+
Test.AssertException<ViewResultAssertionException>(
304+
() =>
305+
{
306+
MyController<MvcController>
307+
.Instance()
308+
.WithoutValidation()
309+
.Calling(c => c.PartialViewWithViewEngine(viewEngine))
310+
.ShouldReturn()
311+
.PartialView(partialView => partialView
312+
.WithViewEngine(null));
313+
},
314+
"When calling PartialViewWithViewEngine action in MvcController expected partial view result engine to be the same as the provided one, but instead received different result.");
315+
}
316+
139317
[Fact]
140318
public void WithViewEngineOfTypeShouldNotThrowExceptionWithValidViewEngineForPartials()
141319
{

0 commit comments

Comments
 (0)