Skip to content

Commit 84df2cb

Browse files
committed
Prepared BaseControllerBuilder to allow Which to work correctly (#282)
1 parent 6fc237f commit 84df2cb

File tree

21 files changed

+251
-91
lines changed

21 files changed

+251
-91
lines changed

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Components/BaseComponentInvocationBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System;
44
using System.Linq.Expressions;
55
using System.Threading.Tasks;
6-
using Internal;
6+
using Internal.Results;
77
using Internal.TestContexts;
88
using Utilities;
99
using System.Reflection;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace MyTested.AspNetCore.Mvc.Internal.Results
2+
{
3+
/// <summary>
4+
/// Represents method result in a generic test builder.
5+
/// </summary>
6+
public class MethodResult
7+
{
8+
}
9+
}

src/MyTested.AspNetCore.Mvc.Abstractions/Internal/VoidMethodResult.cs renamed to src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Results/VoidMethodResult.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
namespace MyTested.AspNetCore.Mvc.Internal
1+
namespace MyTested.AspNetCore.Mvc.Internal.Results
22
{
33
/// <summary>
4-
/// Represents void method result in generic test builder.
4+
/// Represents void method result in a generic test builder.
55
/// </summary>
6-
public class VoidMethodResult
6+
public class VoidMethodResult : MethodResult
77
{
88
/// <summary>
99
/// Gets an instance of <see cref="VoidMethodResult"/>.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Contracts.CaughtExceptions;
77
using CaughtExceptions;
88
using Internal;
9+
using Internal.Results;
910
using Internal.TestContexts;
1011
using ShouldHave;
1112
using Utilities.Validators;
@@ -33,10 +34,10 @@ public IBaseTestBuilderWithInvokedAction ShouldReturnEmpty()
3334
}
3435

3536
/// <inheritdoc />
36-
public IShouldHaveTestBuilder<VoidMethodResult> ShouldHave()
37+
public IShouldHaveTestBuilder<MethodResult> ShouldHave()
3738
{
3839
InvocationValidator.CheckForException(this.CaughtException, this.TestContext.ExceptionMessagePrefix);
39-
return new ShouldHaveTestBuilder<VoidMethodResult>(this.TestContext);
40+
return new ShouldHaveTestBuilder<MethodResult>(this.TestContext);
4041
}
4142

4243
/// <inheritdoc />

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
using Base;
44
using CaughtExceptions;
5-
using Internal;
5+
using Internal.Results;
66

77
/// <summary>
88
/// Used for testing void actions.
@@ -19,7 +19,7 @@ public interface IVoidActionResultTestBuilder : IBaseTestBuilderWithInvokedActio
1919
/// Used for testing the action's additional data - action attributes, HTTP response, view bag and more.
2020
/// </summary>
2121
/// <returns>Test builder of <see cref="IShouldHaveTestBuilder{VoidActionResult}"/> type.</returns>
22-
IShouldHaveTestBuilder<VoidMethodResult> ShouldHave();
22+
IShouldHaveTestBuilder<MethodResult> ShouldHave();
2323

2424
/// <summary>
2525
/// Used for testing whether the action throws exception.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Controllers
2+
{
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Reflection;
6+
using Internal.Contracts;
7+
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Utilities;
10+
using Utilities.Extensions;
11+
12+
/// <content>
13+
/// Used for building the controller which will be tested.
14+
/// </content>
15+
public abstract partial class BaseControllerBuilder<TController, TBuilder>
16+
{
17+
protected override void ProcessAndValidateMethod(LambdaExpression methodCall, MethodInfo methodInfo)
18+
{
19+
this.SetActionDescriptor(methodInfo);
20+
21+
if (this.EnabledModelStateValidation)
22+
{
23+
this.ValidateModelState(methodCall);
24+
}
25+
}
26+
27+
private void SetActionDescriptor(MethodInfo methodInfo)
28+
{
29+
var controllerContext = this.TestContext.ComponentContext;
30+
if (controllerContext.ActionDescriptor?.MethodInfo == null)
31+
{
32+
var controllerActionDescriptorCache = this.Services.GetService<IControllerActionDescriptorCache>();
33+
if (controllerActionDescriptorCache != null)
34+
{
35+
controllerContext.ActionDescriptor
36+
= controllerActionDescriptorCache.TryGetActionDescriptor(methodInfo);
37+
}
38+
}
39+
}
40+
41+
private void ValidateModelState(LambdaExpression actionCall)
42+
{
43+
var arguments = ExpressionParser.ResolveMethodArguments(actionCall).ToArray();
44+
if (arguments.Any())
45+
{
46+
var validator = this.Services.GetRequiredService<IObjectModelValidator>();
47+
arguments.ForEach(argument =>
48+
{
49+
if (argument.Value != null)
50+
{
51+
validator.Validate(this.TestContext.ComponentContext, argument.Value);
52+
}
53+
});
54+
}
55+
}
56+
}
57+
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
using Components;
44
using Contracts.Base;
55
using Internal;
6+
using Internal.Configuration;
67
using Internal.Contracts;
78
using Internal.TestContexts;
89
using Microsoft.AspNetCore.Mvc.Controllers;
910
using Microsoft.Extensions.DependencyInjection;
10-
using System.Linq.Expressions;
11-
using System.Reflection;
1211
using Utilities.Extensions;
1312

1413
/// <summary>
@@ -27,8 +26,12 @@ public abstract partial class BaseControllerBuilder<TController, TBuilder>
2726
/// <param name="testContext"><see cref="ControllerTestContext"/> containing data about the currently executed assertion chain.</param>
2827
protected BaseControllerBuilder(ControllerTestContext testContext)
2928
: base(testContext)
30-
{
31-
}
29+
=> this.EnabledModelStateValidation = ServerTestConfiguration
30+
.Global
31+
.GetControllersConfiguration()
32+
.ModelStateValidation;
33+
34+
public bool EnabledModelStateValidation { get; set; }
3235

3336
protected override string ComponentName => "controller";
3437

@@ -57,10 +60,5 @@ protected override void ActivateComponent()
5760
?.ForEach(a => a
5861
.Exposed()
5962
.Activate(this.TestContext.ComponentContext, this.TestContext.Component));
60-
61-
protected override void ProcessAndValidateMethod(LambdaExpression methodCall, MethodInfo methodInfo)
62-
{
63-
// Intentionally left empty. Not all controller builders allow action execution.
64-
}
6563
}
6664
}
Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.Controllers
22
{
33
using System;
4-
using System.Linq;
54
using System.Linq.Expressions;
6-
using System.Reflection;
75
using System.Threading.Tasks;
86
using Actions;
97
using Contracts.Actions;
10-
using Internal.Contracts;
11-
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
12-
using Microsoft.Extensions.DependencyInjection;
13-
using Utilities;
14-
using Utilities.Extensions;
158

169
/// <content>
1710
/// Used for building the controller which will be tested.
@@ -45,45 +38,5 @@ public IVoidActionResultTestBuilder Calling(Expression<Func<TController, Task>>
4538
this.Invoke(actionCall);
4639
return new VoidActionResultTestBuilder(this.TestContext);
4740
}
48-
49-
protected override void ProcessAndValidateMethod(LambdaExpression methodCall, MethodInfo methodInfo)
50-
{
51-
this.SetActionDescriptor(methodInfo);
52-
53-
if (this.EnabledModelStateValidation)
54-
{
55-
this.ValidateModelState(methodCall);
56-
}
57-
}
58-
59-
private void SetActionDescriptor(MethodInfo methodInfo)
60-
{
61-
var controllerContext = this.TestContext.ComponentContext;
62-
if (controllerContext.ActionDescriptor?.MethodInfo == null)
63-
{
64-
var controllerActionDescriptorCache = this.Services.GetService<IControllerActionDescriptorCache>();
65-
if (controllerActionDescriptorCache != null)
66-
{
67-
controllerContext.ActionDescriptor
68-
= controllerActionDescriptorCache.TryGetActionDescriptor(methodInfo);
69-
}
70-
}
71-
}
72-
73-
private void ValidateModelState(LambdaExpression actionCall)
74-
{
75-
var arguments = ExpressionParser.ResolveMethodArguments(actionCall).ToArray();
76-
if (arguments.Any())
77-
{
78-
var validator = this.Services.GetRequiredService<IObjectModelValidator>();
79-
arguments.ForEach(argument =>
80-
{
81-
if (argument.Value != null)
82-
{
83-
validator.Validate(this.TestContext.ComponentContext, argument.Value);
84-
}
85-
});
86-
}
87-
}
8841
}
8942
}
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.Controllers
22
{
33
using Contracts.Controllers;
4-
using Internal.Configuration;
54
using Internal.TestContexts;
65

76
/// <summary>
@@ -18,24 +17,20 @@ public partial class ControllerBuilder<TController>
1817
/// </summary>
1918
/// <param name="testContext"><see cref="ControllerTestContext"/> containing data about the currently executed assertion chain.</param>
2019
public ControllerBuilder(ControllerTestContext testContext)
21-
: base(testContext)
22-
=> this.EnabledModelStateValidation = ServerTestConfiguration
23-
.Global
24-
.GetControllersConfiguration()
25-
.ModelStateValidation;
26-
27-
public bool EnabledModelStateValidation { get; set; }
28-
29-
/// <inheritdoc />
30-
public IAndControllerBuilder<TController> AndAlso() => this;
20+
: base(testContext)
21+
{
22+
}
3123

3224
/// <inheritdoc />
3325
public IControllerTestBuilder ShouldHave()
3426
{
3527
this.TestContext.ComponentBuildDelegate?.Invoke();
3628
return new ControllerTestBuilder(this.TestContext);
3729
}
38-
30+
31+
/// <inheritdoc />
32+
public IAndControllerBuilder<TController> AndAlso() => this;
33+
3934
protected override IAndControllerBuilder<TController> SetBuilder() => this;
4035
}
4136
}

src/MyTested.AspNetCore.Mvc.Core/MyTested.AspNetCore.Mvc.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<ProjectReference Include="..\MyTested.AspNetCore.Mvc.Controllers.Attributes\MyTested.AspNetCore.Mvc.Controllers.Attributes.csproj" />
3434
<ProjectReference Include="..\MyTested.AspNetCore.Mvc.Controllers\MyTested.AspNetCore.Mvc.Controllers.csproj" />
3535
<ProjectReference Include="..\MyTested.AspNetCore.Mvc.Models\MyTested.AspNetCore.Mvc.Models.csproj" />
36+
<ProjectReference Include="..\MyTested.AspNetCore.Mvc.Pipeline\MyTested.AspNetCore.Mvc.Pipeline.csproj" />
3637
<ProjectReference Include="..\MyTested.AspNetCore.Mvc.Routing\MyTested.AspNetCore.Mvc.Routing.csproj" />
3738
</ItemGroup>
3839

0 commit comments

Comments
 (0)