Skip to content

Commit 4b04145

Browse files
committed
Added OfType method to ViewTestBuilderViewComponentsExtensions and tests (#148)
1 parent bfead15 commit 4b04145

File tree

2 files changed

+83
-8
lines changed

2 files changed

+83
-8
lines changed

src/MyTested.AspNetCore.Mvc.ViewComponents.Results/ViewTestBuilderViewComponentsExtensions.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Builders.ViewComponentResults;
55
using Exceptions;
66
using Microsoft.AspNetCore.Mvc.ViewEngines;
7+
using System;
78
using Utilities;
89

910
/// <summary>
@@ -48,18 +49,30 @@ public static IAndViewTestBuilder WithViewEngine(
4849
public static IAndViewTestBuilder WithViewEngineOfType<TViewEngine>(
4950
this IViewTestBuilder viewTestBuilder)
5051
where TViewEngine : IViewEngine
52+
=> WithViewEngineOfType(viewTestBuilder, typeof(TViewEngine));
53+
54+
/// <summary>
55+
/// Tests whether <see cref="Microsoft.AspNetCore.Mvc.ViewComponents.ViewViewComponentResult"/>
56+
/// has the same <see cref="IViewEngine"/> type as the provided one.
57+
/// </summary>
58+
/// <param name="viewTestBuilder">
59+
/// Instance of <see cref="IViewTestBuilder"/> type.
60+
/// </param>
61+
/// <param name="viewEngineType"></param>
62+
/// <returns>The same <see cref="IAndViewTestBuilder"/>.</returns>
63+
public static IAndViewTestBuilder WithViewEngineOfType(
64+
this IViewTestBuilder viewTestBuilder,Type viewEngineType)
5165
{
5266
var actualBuilder = GetActualBuilder(viewTestBuilder);
5367

5468
var actualViewEngineType = actualBuilder.ViewResult?.ViewEngine?.GetType();
55-
var expectedViewEngineType = typeof(TViewEngine);
5669

5770
if (actualViewEngineType == null
58-
|| Reflection.AreDifferentTypes(expectedViewEngineType, actualViewEngineType))
71+
|| Reflection.AreDifferentTypes(viewEngineType, actualViewEngineType))
5972
{
6073
throw ViewViewComponentResultAssertionException.ForViewEngineType(
6174
actualBuilder.TestContext.ExceptionMessagePrefix,
62-
expectedViewEngineType.ToFriendlyTypeName(),
75+
viewEngineType.ToFriendlyTypeName(),
6376
actualViewEngineType.ToFriendlyTypeName());
6477
}
6578

test/MyTested.AspNetCore.Mvc.ViewComponents.Results.Test/BuildersTests/ViewComponentResultTests/ViewTestBuilderTests.cs

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void WithViewEngineShouldNotThrowExceptionWithValidViewEngine()
2323
}
2424

2525
[Fact]
26-
public void WithViewEngineShouldNotThrowExceptionWithNullViewEngine()
26+
public void WithViewEngineShouldNotThrowExceptionWithNullViewEngineForGeneric()
2727
{
2828
MyViewComponent<ViewResultComponent>
2929
.InvokedWith(c => c.Invoke("Test"))
@@ -32,6 +32,16 @@ public void WithViewEngineShouldNotThrowExceptionWithNullViewEngine()
3232
.WithViewEngineOfType<CompositeViewEngine>();
3333
}
3434

35+
[Fact]
36+
public void WithViewEngineShouldNotThrowExceptionWithNullViewEngine()
37+
{
38+
MyViewComponent<ViewResultComponent>
39+
.InvokedWith(c => c.Invoke("Test"))
40+
.ShouldReturn()
41+
.View("SomeView")
42+
.WithViewEngineOfType(typeof(CompositeViewEngine));
43+
}
44+
3545
[Fact]
3646
public void WithViewEngineShouldThrowExceptionWithInvalidViewEngine()
3747
{
@@ -48,7 +58,7 @@ public void WithViewEngineShouldThrowExceptionWithInvalidViewEngine()
4858
}
4959

5060
[Fact]
51-
public void WithViewEngineOfTypeShouldNotThrowExceptionWithValidViewEngine()
61+
public void WithViewEngineOfTypeShouldNotThrowExceptionWithValidViewEngineForGeneric()
5262
{
5363
MyViewComponent<ViewEngineComponent>
5464
.InvokedWith(c => c.Invoke(new CustomViewEngine()))
@@ -58,7 +68,17 @@ public void WithViewEngineOfTypeShouldNotThrowExceptionWithValidViewEngine()
5868
}
5969

6070
[Fact]
61-
public void WithViewEngineOfTypeShouldThrowExceptionWithInvalidViewEngine()
71+
public void WithViewEngineOfTypeShouldNotThrowExceptionWithValidViewEngine()
72+
{
73+
MyViewComponent<ViewEngineComponent>
74+
.InvokedWith(c => c.Invoke(new CustomViewEngine()))
75+
.ShouldReturn()
76+
.View()
77+
.WithViewEngineOfType(typeof(CustomViewEngine));
78+
}
79+
80+
[Fact]
81+
public void WithViewEngineOfTypeShouldThrowExceptionWithInvalidViewEngineForGeneric()
6282
{
6383
Test.AssertException<ViewViewComponentResultAssertionException>(
6484
() =>
@@ -73,7 +93,22 @@ public void WithViewEngineOfTypeShouldThrowExceptionWithInvalidViewEngine()
7393
}
7494

7595
[Fact]
76-
public void WithViewEngineOfTypeShouldNotThrowExceptionWithNullViewEngine()
96+
public void WithViewEngineOfTypeShouldThrowExceptionWithInvalidViewEngine()
97+
{
98+
Test.AssertException<ViewViewComponentResultAssertionException>(
99+
() =>
100+
{
101+
MyViewComponent<ViewEngineComponent>
102+
.InvokedWith(c => c.Invoke(new CustomViewEngine()))
103+
.ShouldReturn()
104+
.View()
105+
.WithViewEngineOfType(typeof(IViewEngine));
106+
},
107+
"When invoking ViewEngineComponent expected view result ViewEngine to be of IViewEngine type, but instead received CustomViewEngine.");
108+
}
109+
110+
[Fact]
111+
public void WithViewEngineOfTypeShouldNotThrowExceptionWithNullViewEngineForGeneric()
77112
{
78113
Test.AssertException<ViewViewComponentResultAssertionException>(
79114
() =>
@@ -88,7 +123,22 @@ public void WithViewEngineOfTypeShouldNotThrowExceptionWithNullViewEngine()
88123
}
89124

90125
[Fact]
91-
public void AndAlsoShouldWorkCorrectly()
126+
public void WithViewEngineOfTypeShouldNotThrowExceptionWithNullViewEngine()
127+
{
128+
Test.AssertException<ViewViewComponentResultAssertionException>(
129+
() =>
130+
{
131+
MyViewComponent<ViewResultComponent>
132+
.InvokedWith(c => c.Invoke(null))
133+
.ShouldReturn()
134+
.View()
135+
.WithViewEngineOfType(typeof(CustomViewEngine));
136+
},
137+
"When invoking ViewResultComponent expected view result ViewEngine to be of CustomViewEngine type, but instead received CompositeViewEngine.");
138+
}
139+
140+
[Fact]
141+
public void AndAlsoShouldWorkCorrectlyForGeneric()
92142
{
93143
MyViewComponent<ViewResultComponent>
94144
.InvokedWith(c => c.Invoke("All"))
@@ -98,5 +148,17 @@ public void AndAlsoShouldWorkCorrectly()
98148
.AndAlso()
99149
.WithModelOfType<ResponseModel>();
100150
}
151+
152+
[Fact]
153+
public void AndAlsoShouldWorkCorrectly()
154+
{
155+
MyViewComponent<ViewResultComponent>
156+
.InvokedWith(c => c.Invoke("All"))
157+
.ShouldReturn()
158+
.View("SomeView")
159+
.WithViewEngineOfType(typeof(CompositeViewEngine))
160+
.AndAlso()
161+
.WithModelOfType(typeof(ResponseModel));
162+
}
101163
}
102164
}

0 commit comments

Comments
 (0)