Skip to content

Commit c747832

Browse files
committed
Implemented all View Component test builders (#66)
1 parent b10ec81 commit c747832

File tree

5 files changed

+89
-23
lines changed

5 files changed

+89
-23
lines changed

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Base/BaseTestBuilderWithResponseModel.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,23 @@ public IAndModelDetailsTestBuilder<TModel> WithModel<TModel>(TModel expectedMode
6868
this.TestContext.Model = actualModel;
6969
return new ModelDetailsTestBuilder<TModel>(this.TestContext);
7070
}
71+
72+
protected virtual TModel GetActualModel<TModel>()
73+
{
74+
try
75+
{
76+
return (TModel)this.GetActualModel();
77+
}
78+
catch (InvalidCastException)
79+
{
80+
throw new ResponseModelAssertionException(string.Format(
81+
"{0} response model to be a {1}, but instead received null.",
82+
this.TestContext.ExceptionMessagePrefix,
83+
typeof(TModel).ToFriendlyTypeName()));
84+
}
85+
}
7186

72-
protected abstract TModel GetActualModel<TModel>();
87+
protected abstract object GetActualModel();
7388

7489
protected abstract Type GetReturnType();
7590
}

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Base/BaseTestBuilderWithResponseModel.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -173,22 +173,7 @@ protected void ValidateOutputFormatters(params IOutputFormatter[] outputFormatte
173173
/// <param name="expectedValue">Expected value of the tested property.</param>
174174
/// <param name="actualValue">Actual value of the tested property.</param>
175175
protected abstract void ThrowNewFailedValidationException(string propertyName, string expectedValue, string actualValue);
176-
177-
protected override TModel GetActualModel<TModel>()
178-
{
179-
try
180-
{
181-
return (TModel)this.GetActualModel();
182-
}
183-
catch (InvalidCastException)
184-
{
185-
throw new ResponseModelAssertionException(string.Format(
186-
"{0} response model to be a {1}, but instead received null.",
187-
this.TestContext.ExceptionMessagePrefix,
188-
typeof(TModel).ToFriendlyTypeName()));
189-
}
190-
}
191-
176+
192177
private ObjectResult GetObjectResult()
193178
{
194179
var objectResult = this.TestContext.MethodResult as ObjectResult;
@@ -202,7 +187,7 @@ private ObjectResult GetObjectResult()
202187
return objectResult;
203188
}
204189

205-
protected virtual object GetActualModel()
190+
protected override object GetActualModel()
206191
{
207192
return (this.TestContext.MethodResult as ObjectResult)?.Value;
208193
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.ViewComponentResults
2+
{
3+
/// <summary>
4+
/// Contains AndAlso() method allowing additional assertions after the view component view result tests.
5+
/// </summary>
6+
public interface IAndViewTestBuilder : IViewTestBuilder
7+
{
8+
/// <summary>
9+
/// Method allowing additional assertions after the view component view result tests.
10+
/// </summary>
11+
/// <returns>Test builder of <see cref="IViewTestBuilder"/>.</returns>
12+
IViewTestBuilder AndAlso();
13+
}
14+
}

src/MyTested.AspNetCore.Mvc.ViewComponents/Builders/ViewComponentResults/ViewTestBuilder.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,59 @@
44
using Base;
55
using Contracts.ViewComponentResults;
66
using Internal.TestContexts;
7+
using Microsoft.AspNetCore.Mvc.ViewComponents;
8+
using Microsoft.AspNetCore.Mvc.ViewEngines;
9+
using Utilities;
10+
using Exceptions;
711

8-
public class ViewTestBuilder : BaseTestBuilderWithResponseModel, IViewTestBuilder
12+
public class ViewTestBuilder : BaseTestBuilderWithResponseModel, IAndViewTestBuilder
913
{
14+
private readonly ViewViewComponentResult viewResult;
15+
1016
public ViewTestBuilder(ActionTestContext testContext)
1117
: base(testContext)
1218
{
19+
this.viewResult = testContext.MethodResultAs<ViewViewComponentResult>();
1320
}
1421

15-
protected override TModel GetActualModel<TModel>()
22+
/// <inheritdoc />
23+
public IAndViewTestBuilder WithViewEngine(IViewEngine viewEngine)
1624
{
17-
throw new NotImplementedException();
25+
var actualViewEngine = this.viewResult.ViewEngine;
26+
if (viewEngine != actualViewEngine)
27+
{
28+
throw ViewViewComponentResultAssertionException
29+
.ForViewEngineEquality(this.TestContext.ExceptionMessagePrefix);
30+
}
31+
32+
return this;
1833
}
1934

20-
protected override Type GetReturnType()
35+
/// <inheritdoc />
36+
public IAndViewTestBuilder WithViewEngineOfType<TViewEngine>()
37+
where TViewEngine : IViewEngine
2138
{
22-
throw new NotImplementedException();
39+
var actualViewEngineType = this.viewResult?.ViewEngine?.GetType();
40+
var expectedViewEngineType = typeof(TViewEngine);
41+
42+
if (actualViewEngineType == null
43+
|| Reflection.AreDifferentTypes(expectedViewEngineType, actualViewEngineType))
44+
{
45+
throw ViewViewComponentResultAssertionException.ForViewEngineType(
46+
this.TestContext.ExceptionMessagePrefix,
47+
expectedViewEngineType.ToFriendlyTypeName(),
48+
actualViewEngineType.ToFriendlyTypeName());
49+
}
50+
51+
return this;
2352
}
53+
54+
/// <inheritdoc />
55+
public IViewTestBuilder AndAlso() => this;
56+
57+
protected override object GetActualModel()
58+
=> this.TestContext.MethodResultAs<ViewViewComponentResult>()?.ViewData?.Model;
59+
60+
protected override Type GetReturnType() => this.GetActualModel()?.GetType();
2461
}
2562
}

src/MyTested.AspNetCore.Mvc.ViewComponents/Exceptions/ViewViewComponentResultAssertionException.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ public static ViewViewComponentResultAssertionException ForNameEquality(
1818
TestHelper.GetFriendlyName(expectedViewName),
1919
TestHelper.GetFriendlyName(actualViewName)));
2020

21+
public static ViewViewComponentResultAssertionException ForViewEngineEquality(
22+
string messagePrefix)
23+
=> new ViewViewComponentResultAssertionException(string.Format(
24+
$"{messagePrefix} view result ViewEngine to be the same as the provided one, but instead received different result."));
25+
26+
public static ViewViewComponentResultAssertionException ForViewEngineType(
27+
string messagePrefix,
28+
string expectedViewEngineType,
29+
string actualViewEngineType)
30+
=> new ViewViewComponentResultAssertionException(string.Format(
31+
"{0} view result ViewEngine to be of {1} type, but instead received {2}.",
32+
messagePrefix,
33+
expectedViewEngineType,
34+
actualViewEngineType));
35+
2136
/// <summary>
2237
/// Initializes a new instance of the <see cref="ViewViewComponentResultAssertionException"/> class.
2338
/// </summary>

0 commit comments

Comments
 (0)