Skip to content

Commit 2488993

Browse files
committed
Added unit tests for ViewComponentTestBuilderExtensions.
1 parent 34a0ebf commit 2488993

File tree

1 file changed

+111
-1
lines changed

1 file changed

+111
-1
lines changed

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

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,57 @@ public void ShouldReturnViewComponentShouldNotThrowExceptionWithCorrectViewCompo
2020
.WithName("TestComponent"));
2121
}
2222

23+
[Fact]
24+
public void ShouldReturnViewComponentShouldNotThrowExceptionWithViewComponentName()
25+
{
26+
MyController<MvcController>
27+
.Instance()
28+
.Calling(c => c.ViewComponent("MyComponent"))
29+
.ShouldReturn()
30+
.ViewComponent(viewComponent => viewComponent
31+
.WithName("MyComponent"));
32+
}
33+
34+
[Fact]
35+
public void ShouldReturnViewComponentShouldNotThrowExceptionWithEmptyViewComponentName()
36+
{
37+
MyController<MvcController>
38+
.Instance()
39+
.Calling(c => c.ViewComponent(string.Empty))
40+
.ShouldReturn()
41+
.ViewComponent(viewComponent => viewComponent
42+
.WithName(string.Empty));
43+
}
44+
45+
[Fact]
46+
public void ShouldReturnViewComponentShouldNotThrowExceptionWithViewComponentNameAndPassAssertions()
47+
{
48+
MyController<MvcController>
49+
.Instance()
50+
.Calling(c => c.ViewComponent("MyComponent"))
51+
.ShouldReturn()
52+
.ViewComponent(viewComponent => viewComponent
53+
.WithName("MyComponent")
54+
.AndAlso()
55+
.Passing(vc =>
56+
{
57+
Assert.NotNull(vc);
58+
Assert.NotEmpty(vc.ViewComponentName);
59+
Assert.True(typeof(ViewComponentResult) == vc.GetType());
60+
}));
61+
}
62+
63+
[Fact]
64+
public void ShouldReturnViewComponentShouldNotThrowExceptionWithNull()
65+
{
66+
MyController<MvcController>
67+
.Instance()
68+
.Calling(c => c.CustomViewComponentResult())
69+
.ShouldReturn()
70+
.ViewComponent(viewComponent => viewComponent
71+
.WithName(null));
72+
}
73+
2374
[Fact]
2475
public void ShouldReturnViewComponentShouldThrowExceptionWithIncorrectViewComponentWithName()
2576
{
@@ -52,6 +103,22 @@ public void ShouldReturnViewComponentShouldThrowExceptionWithIncorrectViewCompon
52103
"When calling ViewComponentResultByName action in MvcController expected view component result to be 'Incorrect', but instead received 'TestComponent'.");
53104
}
54105

106+
[Fact]
107+
public void ShouldReturnViewComponentShouldThrowExceptionWithDifferentCaseViewComponentName()
108+
{
109+
Test.AssertException<ViewResultAssertionException>(
110+
() =>
111+
{
112+
MyController<MvcController>
113+
.Instance()
114+
.Calling(c => c.ViewComponent("Incorrect"))
115+
.ShouldReturn()
116+
.ViewComponent(viewComponent => viewComponent
117+
.WithName("incorrect"));
118+
},
119+
"When calling ViewComponentResultByName action in MvcController expected view component result to be 'incorrect', but instead received 'Incorrect'.");
120+
}
121+
55122
[Fact]
56123
public void ShouldReturnViewComponentShouldNotThrowExceptionWithCorrectViewComponentType()
57124
{
@@ -62,7 +129,18 @@ public void ShouldReturnViewComponentShouldNotThrowExceptionWithCorrectViewCompo
62129
.ViewComponent(viewComponent => viewComponent
63130
.OfType(typeof(CustomViewComponent)));
64131
}
65-
132+
133+
[Fact]
134+
public void ShouldReturnViewComponentShouldNotThrowExceptionWithNullComponentType()
135+
{
136+
MyController<MvcController>
137+
.Instance()
138+
.Calling(c => c.CustomViewComponentResult())
139+
.ShouldReturn()
140+
.ViewComponent(viewComponent => viewComponent
141+
.OfType(null));
142+
}
143+
66144
[Fact]
67145
public void ShouldReturnViewComponentShouldNotThrowExceptionWithCorrectViewComponentTypeAsGeneric()
68146
{
@@ -105,5 +183,37 @@ public void ShouldReturnViewComponentShouldThrowExceptionWithIncorrectViewCompon
105183
},
106184
"When calling ViewComponentResultByType action in MvcController expected view component result to be 'ViewComponent', but instead received 'CustomViewComponent'.");
107185
}
186+
187+
[Fact]
188+
public void ShouldReturnViewComponentShouldThrowExceptionWithNullComponentType()
189+
{
190+
Test.AssertException<ViewResultAssertionException>(
191+
() =>
192+
{
193+
MyController<MvcController>
194+
.Instance()
195+
.Calling(c => c.ViewComponentResultByType())
196+
.ShouldReturn()
197+
.ViewComponent(viewComponent => viewComponent
198+
.OfType(null));
199+
},
200+
"When calling ViewComponentResultByType action in MvcController expected view component result to be 'null', but instead received 'CustomViewComponent'.");
201+
}
202+
203+
[Fact]
204+
public void ShouldReturnViewComponentShouldThrowExceptionWithIncorrectViewComponentTypeAsGeneric()
205+
{
206+
Test.AssertException<ViewResultAssertionException>(
207+
() =>
208+
{
209+
MyController<MvcController>
210+
.Instance()
211+
.Calling(c => c.ViewComponent("Test"))
212+
.ShouldReturn()
213+
.ViewComponent(viewComponent => viewComponent
214+
.OfType<CustomViewComponent>());
215+
},
216+
"When calling ViewComponent action in MvcController expected view component result to be 'CustomViewComponent', but instead received 'null'.");
217+
}
108218
}
109219
}

0 commit comments

Comments
 (0)