Skip to content

Commit 7643730

Browse files
committed
Added unit tests for ViewComponent testing (#66)
1 parent f860188 commit 7643730

File tree

55 files changed

+2232
-92
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2232
-92
lines changed

samples/MusicStore/MusicStore.Test/Controllers/HomeControllerTest.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ public void IndexShouldReturnTopSellingAlbumsAndSaveThenIntoCache()
3030
.ShouldReturn()
3131
.View()
3232
.WithModelOfType<List<Album>>()
33-
.Passing(albums =>
34-
{
35-
Assert.Equal(6, albums.Count);
36-
});
33+
.Passing(albums => Assert.Equal(6, albums.Count));
3734
}
3835

3936
[Fact]

samples/MusicStore/MusicStore.Test/TestStartup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public void ConfigureTestServices(IServiceCollection services)
3737
TestHelper.HttpFeatureRegistrationPlugins.Add(new SessionTestPlugin());
3838

3939
TestHelper.ShouldPassForPlugins.Add(new AbstractionsTestPlugin());
40+
TestHelper.ShouldPassForPlugins.Add(new HttpTestPlugin());
4041
TestHelper.ShouldPassForPlugins.Add(new ControllersTestPlugin());
4142

4243
services

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Invocations/IBaseShouldReturnTestBuilder.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public interface IBaseShouldReturnTestBuilder<TInvocationResult, TBuilder>
2727
/// Tests whether the result is of the provided type.
2828
/// </summary>
2929
/// <typeparam name="TResult">Expected response type.</typeparam>
30-
/// <returns>Test builder of <see cref="IModelDetailsTestBuilder{TInvocationResult}"/> type.</returns>
31-
IAndModelDetailsTestBuilder<TInvocationResult> ResultOfType<TResult>();
30+
/// <returns>Test builder of <see cref="IModelDetailsTestBuilder{TResult}"/> type.</returns>
31+
IAndModelDetailsTestBuilder<TResult> ResultOfType<TResult>();
3232

3333
/// <summary>
3434
/// Tests whether the result is of the provided type.
@@ -42,8 +42,7 @@ public interface IBaseShouldReturnTestBuilder<TInvocationResult, TBuilder>
4242
/// </summary>
4343
/// <typeparam name="TResult">Expected response type.</typeparam>
4444
/// <param name="model">Expected return object.</param>
45-
/// <returns>Test builder of <see cref="IModelDetailsTestBuilder{TInvocationResult}"/> type.</returns>
46-
IAndModelDetailsTestBuilder<TInvocationResult> Result<TResult>(TResult model);
47-
45+
/// <returns>Test builder of <see cref="IModelDetailsTestBuilder{TResult}"/> type.</returns>
46+
IAndModelDetailsTestBuilder<TResult> Result<TResult>(TResult model);
4847
}
4948
}

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Models/ModelDetailsTestBuilder.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ public IAndTestBuilder Passing(Func<TModel, bool> predicate)
3838
if (!predicate(this.Model))
3939
{
4040
throw new ResponseModelAssertionException(string.Format(
41-
"When calling {0} action in {1} expected response model {2} to pass the given predicate, but it failed.",
42-
this.TestContext.MethodName,
43-
this.TestContext.Component.GetName(),
41+
"{0} response model {1} to pass the given predicate, but it failed.",
42+
this.TestContext.ExceptionMessagePrefix,
4443
typeof(TModel).ToFriendlyTypeName()));
4544
}
4645

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/TestHelper.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using Services;
1111
using TestContexts;
1212
using Utilities;
13+
using Microsoft.AspNetCore.Mvc;
14+
using Microsoft.AspNetCore.Mvc.Infrastructure;
1315

1416
public static class TestHelper
1517
{
@@ -50,6 +52,15 @@ public static void SetHttpContextToAccessor(HttpContext httpContext)
5052
}
5153
}
5254

55+
public static void SetActionContextToAccessor(ActionContext actionContext)
56+
{
57+
var actionContextAccessor = TestServiceProvider.GetService<IActionContextAccessor>();
58+
if (actionContextAccessor != null)
59+
{
60+
actionContextAccessor.ActionContext = actionContext;
61+
}
62+
}
63+
5364
/// <summary>
5465
/// Tries to create instance of the provided type. Returns null if not successful.
5566
/// </summary>

src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/Validators/InvocationResultValidator.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ public static void ValidateInvocationResult<TResult>(ComponentTestContext testCo
9191
testContext.Model = model;
9292
}
9393

94-
public static TResult GetInvocationResult<TResult>(ComponentTestContext testContext)
94+
public static TResult GetInvocationResult<TResult>(
95+
ComponentTestContext testContext,
96+
bool canBeAssignable = false)
9597
where TResult : class
9698
{
97-
ValidateInvocationResultType<TResult>(testContext);
99+
ValidateInvocationResultType<TResult>(testContext, canBeAssignable);
98100
return testContext.MethodResult as TResult;
99101
}
100102

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Actions/ShouldReturn/ShouldReturnResult.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ public IAndModelDetailsTestBuilder<TActionResult> ResultOfType(Type returnType)
1818
}
1919

2020
/// <inheritdoc />
21-
public IAndModelDetailsTestBuilder<TActionResult> ResultOfType<TResult>()
21+
public IAndModelDetailsTestBuilder<TResult> ResultOfType<TResult>()
2222
{
2323
InvocationResultValidator.ValidateInvocationResultType<TResult>(this.TestContext, true);
24-
return new ModelDetailsTestBuilder<TActionResult>(this.TestContext);
24+
return new ModelDetailsTestBuilder<TResult>(this.TestContext);
2525
}
2626

2727
/// <inheritdoc />
28-
public IAndModelDetailsTestBuilder<TActionResult> Result<TResult>(TResult model)
28+
public IAndModelDetailsTestBuilder<TResult> Result<TResult>(TResult model)
2929
{
3030
InvocationResultValidator.ValidateInvocationResult(this.TestContext, model);
31-
return new ModelDetailsTestBuilder<TActionResult>(this.TestContext);
31+
return new ModelDetailsTestBuilder<TResult>(this.TestContext);
3232
}
3333
}
3434
}

src/MyTested.AspNetCore.Mvc.Controllers/Internal/Controllers/ControllerContextMock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private void PrepareControllerContext(ActionContext actionContext)
3636
this.RouteData = actionContext.RouteData;
3737
this.ValueProviderFactories = this.ValueProviderFactories ?? new List<IValueProviderFactory>();
3838

39-
ControllerTestHelper.SetActionContextToAccessor(this);
39+
TestHelper.SetActionContextToAccessor(this);
4040
}
4141
}
4242
}

src/MyTested.AspNetCore.Mvc.Controllers/Internal/Controllers/ControllerPropertyHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public class ControllerPropertyHelper : PropertyHelper
99
private static readonly ConcurrentDictionary<Type, ControllerPropertyHelper> ControllerPropertiesCache =
1010
new ConcurrentDictionary<Type, ControllerPropertyHelper>();
1111

12-
private Func<object, ControllerContext> controllerContextGetter;
13-
private Func<object, ActionContext> actionContextGetter;
12+
private Func<object, ControllerContext> controllerContextGetter; // DO I NEED THESE???
13+
private Func<object, ActionContext> actionContextGetter; // DO I NEED THESE?
1414

1515
public ControllerPropertyHelper(Type controllerType)
1616
: base (controllerType)

src/MyTested.AspNetCore.Mvc.Controllers/Internal/Controllers/ControllerTestHelper.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)