Skip to content

Commit abd9fb5

Browse files
committed
All unit tests now run successfully with the generic version of ShouldPassFor (closes #181)
1 parent 39f59c6 commit abd9fb5

File tree

19 files changed

+81
-77
lines changed

19 files changed

+81
-77
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.Base
22
{
33
using System;
4+
using And;
45
using Contracts.And;
56
using Contracts.Base;
67
using Exceptions;
78
using Internal;
89
using Internal.TestContexts;
910
using Utilities;
1011
using Utilities.Validators;
11-
using And;
1212

1313
/// <summary>
1414
/// Base class for all test builders with component.
@@ -25,6 +25,7 @@ protected BaseTestBuilderWithComponent(ComponentTestContext testContext)
2525
: base(testContext)
2626
{
2727
this.TestContext = testContext;
28+
this.BuildComponentAction += () => { };
2829
}
2930

3031
/// <summary>
@@ -45,18 +46,25 @@ private set
4546
}
4647
}
4748

49+
protected Action BuildComponentAction { get; set; }
50+
4851
/// <inheritdoc />
4952
public IAndTestBuilder ShouldPassForThe<TComponent>(Action<TComponent> assertions)
5053
where TComponent : class
5154
{
55+
this.BuildComponentAction();
56+
5257
assertions(TestHelper.TryGetShouldPassForValue<TComponent>(this.TestContext));
58+
5359
return new AndTestBuilder(this.TestContext);
5460
}
5561

5662
/// <inheritdoc />
5763
public IAndTestBuilder ShouldPassForThe<TComponent>(Func<TComponent, bool> predicate)
5864
where TComponent : class
5965
{
66+
this.BuildComponentAction();
67+
6068
if (!predicate(TestHelper.TryGetShouldPassForValue<TComponent>(this.TestContext)))
6169
{
6270
throw new InvalidAssertionException($"Expected {typeof(TComponent).ToFriendlyTypeName()} to pass the given predicate but it failed.");
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Components
2+
{
3+
using Base;
4+
using Contracts.Base;
5+
using Internal.TestContexts;
6+
7+
public abstract class BaseComponentBuilder<TBuilder> : BaseTestBuilderWithComponentBuilder<TBuilder>
8+
where TBuilder : IBaseTestBuilder
9+
{
10+
public BaseComponentBuilder(ComponentTestContext testContext)
11+
: base(testContext)
12+
{
13+
this.BuildComponentAction += this.BuildComponentIfNotExists;
14+
}
15+
16+
protected abstract void BuildComponentIfNotExists();
17+
}
18+
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,20 @@ public static TComponent TryGetShouldPassForValue<TComponent>(ComponentTestConte
5555
var result = testContext.ComponentAs<TComponent>()
5656
?? testContext.MethodResultAs<TComponent>()
5757
?? testContext.ModelAs<TComponent>();
58+
59+
if (result != null)
60+
{
61+
return result;
62+
}
5863

5964
foreach (var shouldPassForPlugin in ShouldPassForPlugins)
6065
{
66+
result = shouldPassForPlugin.TryGetValue(typeof(TComponent), testContext) as TComponent;
67+
6168
if (result != null)
6269
{
6370
return result;
6471
}
65-
66-
result = shouldPassForPlugin.TryGetValue(typeof(TComponent), testContext) as TComponent;
6772
}
6873

6974
throw new InvalidOperationException($"{typeof(TComponent).ToFriendlyTypeName()} could not be resolved for the 'ShouldPassForThe<TComponent>' method call.");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace MyTested.AspNetCore.Mvc.Internal
2+
{
3+
/// <summary>
4+
/// Represents void method result in generic test builder.
5+
/// </summary>
6+
public class VoidMethodResult
7+
{
8+
/// <summary>
9+
/// Gets an instance of <see cref="VoidMethodResult"/>.
10+
/// </summary>
11+
/// <returns>Void method result.</returns>
12+
/// <value>Instance of <see cref="VoidMethodResult"/>.</value>
13+
public static VoidMethodResult Instance { get; } = new VoidMethodResult();
14+
}
15+
}

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Actions/VoidActionResultTestBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public IBaseTestBuilderWithInvokedAction ShouldReturnEmpty()
3030
}
3131

3232
/// <inheritdoc />
33-
public IShouldHaveTestBuilder<VoidActionResult> ShouldHave()
33+
public IShouldHaveTestBuilder<VoidMethodResult> ShouldHave()
3434
{
35-
return new ShouldHaveTestBuilder<VoidActionResult>(this.TestContext);
35+
return new ShouldHaveTestBuilder<VoidMethodResult>(this.TestContext);
3636
}
3737

3838
/// <inheritdoc />

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Contracts/Actions/IVoidActionResultTestBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public interface IVoidActionResultTestBuilder : IBaseTestBuilderWithInvokedActio
1818
/// Used for testing the action's additional data - action attributes, HTTP response, view bag and more.
1919
/// </summary>
2020
/// <returns>Test builder of <see cref="IShouldHaveTestBuilder{VoidActionResult}"/> type.</returns>
21-
IShouldHaveTestBuilder<VoidActionResult> ShouldHave();
21+
IShouldHaveTestBuilder<VoidMethodResult> ShouldHave();
2222

2323
/// <summary>
2424
/// Used for testing whether the action throws exception.

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Controllers/ControllerActionCallBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public IVoidActionResultTestBuilder Calling(Expression<Action<TController>> acti
6969
this.TestContext.MethodName = actionName;
7070
this.TestContext.MethodCall = actionCall;
7171
this.TestContext.CaughtException = caughtException;
72-
this.TestContext.MethodResult = VoidActionResult.Instance;
72+
this.TestContext.MethodResult = VoidMethodResult.Instance;
7373

7474
return new VoidActionResultTestBuilder(this.TestContext);
7575
}
@@ -89,7 +89,7 @@ public IVoidActionResultTestBuilder Calling(Expression<Func<TController, Task>>
8989
}
9090

9191
this.TestContext.Apply(actionInfo);
92-
this.TestContext.MethodResult = VoidActionResult.Instance;
92+
this.TestContext.MethodResult = VoidMethodResult.Instance;
9393

9494
return new VoidActionResultTestBuilder(this.TestContext);
9595
}
@@ -114,7 +114,7 @@ private ActionTestContext<TActionResult> GetAndValidateActionResult<TActionResul
114114

115115
private string GetAndValidateAction(LambdaExpression actionCall)
116116
{
117-
this.BuildControllerIfNotExists();
117+
this.BuildComponentAction();
118118

119119
this.TestContext.MethodCall = actionCall;
120120
this.TestContext.PreMethodInvocationAction?.Invoke();

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Controllers/ControllerBuilder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using System;
44
using Base;
5+
using Components;
56
using Contracts.Controllers;
67
using Internal.Application;
78
using Internal.Contracts;
@@ -17,7 +18,7 @@
1718
/// Used for building the controller which will be tested.
1819
/// </summary>
1920
/// <typeparam name="TController">Class representing ASP.NET Core MVC controller.</typeparam>
20-
public partial class ControllerBuilder<TController> : BaseTestBuilderWithComponentBuilder<IAndControllerBuilder<TController>>, IAndControllerBuilder<TController>
21+
public partial class ControllerBuilder<TController> : BaseComponentBuilder<IAndControllerBuilder<TController>>, IAndControllerBuilder<TController>
2122
where TController : class
2223
{
2324
private ControllerTestContext testContext;
@@ -45,7 +46,7 @@ private TController Controller
4546
{
4647
get
4748
{
48-
this.BuildControllerIfNotExists();
49+
this.BuildComponentAction();
4950
return this.TestContext.ComponentAs<TController>();
5051
}
5152
}
@@ -81,7 +82,7 @@ public IAndControllerBuilder<TController> AndAlso()
8182
/// <inheritdoc />
8283
public IControllerTestBuilder ShouldHave()
8384
{
84-
this.BuildControllerIfNotExists();
85+
this.BuildComponentAction();
8586
return new ControllerTestBuilder(this.TestContext);
8687
}
8788

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Controllers/ControllerSetupBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public IAndControllerBuilder<TController> WithSetup(Action<TController> controll
5353
return this;
5454
}
5555

56-
private void BuildControllerIfNotExists()
56+
protected override void BuildComponentIfNotExists()
5757
{
5858
if (!this.isPreparedForTesting)
5959
{

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

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

0 commit comments

Comments
 (0)