Skip to content

Commit aade147

Browse files
committed
Extracted BaseActionResultTestBuilder (#282)
1 parent a3580a4 commit aade147

File tree

11 files changed

+98
-94
lines changed

11 files changed

+98
-94
lines changed

src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/MemoryCacheBuilder.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ public class MemoryCacheBuilder : IAndMemoryCacheBuilder
1616
/// Initializes a new instance of the <see cref="MemoryCacheBuilder"/> class.
1717
/// </summary>
1818
/// <param name="services"><see cref="IServiceProvider"/> providing the current <see cref="IMemoryCache"/>.</param>
19-
public MemoryCacheBuilder(IServiceProvider services)
20-
{
21-
this.MemoryCache = services.GetRequiredService<IMemoryCache>();
22-
}
19+
public MemoryCacheBuilder(IServiceProvider services)
20+
=> this.MemoryCache = services.GetRequiredService<IMemoryCache>();
2321

2422
/// <summary>
2523
/// Gets the mocked <see cref="IMemoryCache"/>.

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.Actions
22
{
3-
using Base;
43
using Contracts.Actions;
5-
using Contracts.CaughtExceptions;
6-
using CaughtExceptions;
74
using Internal;
85
using Internal.TestContexts;
9-
using ShouldHave;
106
using ShouldReturn;
117
using Utilities.Validators;
128

@@ -15,7 +11,8 @@
1511
/// </summary>
1612
/// <typeparam name="TActionResult">Result from invoked action in ASP.NET Core MVC controller.</typeparam>
1713
public class ActionResultTestBuilder<TActionResult>
18-
: BaseTestBuilderWithActionResult<TActionResult>, IActionResultTestBuilder<TActionResult>
14+
: BaseActionResultTestBuilder<TActionResult>,
15+
IActionResultTestBuilder<TActionResult>
1916
{
2017
/// <summary>
2118
/// Initializes a new instance of the <see cref="ActionResultTestBuilder{TActionResult}"/> class.
@@ -26,21 +23,6 @@ public ActionResultTestBuilder(ControllerTestContext testContext)
2623
{
2724
}
2825

29-
/// <inheritdoc />
30-
public IShouldHaveTestBuilder<TActionResult> ShouldHave()
31-
{
32-
InvocationValidator.CheckForException(this.CaughtException, this.TestContext.ExceptionMessagePrefix);
33-
return new ShouldHaveTestBuilder<TActionResult>(this.TestContext);
34-
}
35-
36-
/// <inheritdoc />
37-
public IShouldThrowTestBuilder ShouldThrow()
38-
{
39-
TestHelper.ExecuteTestCleanup();
40-
InvocationValidator.CheckForNullException(this.CaughtException, this.TestContext.ExceptionMessagePrefix);
41-
return new ShouldThrowTestBuilder(this.TestContext);
42-
}
43-
4426
/// <inheritdoc />
4527
public IShouldReturnTestBuilder<TActionResult> ShouldReturn()
4628
{
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Actions
2+
{
3+
using Base;
4+
using CaughtExceptions;
5+
using Contracts.Actions;
6+
using Contracts.CaughtExceptions;
7+
using Internal;
8+
using Internal.TestContexts;
9+
using ShouldHave;
10+
using Utilities.Validators;
11+
12+
/// <summary>
13+
/// Base class for all action result test builders.
14+
/// </summary>
15+
/// <typeparam name="TActionResult">Result from invoked action in ASP.NET Core MVC controller.</typeparam>
16+
public abstract class BaseActionResultTestBuilder<TActionResult>
17+
: BaseTestBuilderWithActionResult<TActionResult>,
18+
IBaseActionResultTestBuilder<TActionResult>
19+
{
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="BaseActionResultTestBuilder{TActionResult}"/> class.
22+
/// </summary>
23+
/// <param name="testContext"><see cref="ControllerTestContext"/> containing data about the currently executed assertion chain.</param>
24+
protected BaseActionResultTestBuilder(ControllerTestContext testContext)
25+
: base(testContext)
26+
{
27+
}
28+
29+
/// <inheritdoc />
30+
public IShouldHaveTestBuilder<TActionResult> ShouldHave()
31+
{
32+
InvocationValidator.CheckForException(this.CaughtException, this.TestContext.ExceptionMessagePrefix);
33+
return new ShouldHaveTestBuilder<TActionResult>(this.TestContext);
34+
}
35+
36+
/// <inheritdoc />
37+
public IShouldThrowTestBuilder ShouldThrow()
38+
{
39+
TestHelper.ExecuteTestCleanup();
40+
InvocationValidator.CheckForNullException(this.CaughtException, this.TestContext.ExceptionMessagePrefix);
41+
return new ShouldThrowTestBuilder(this.TestContext);
42+
}
43+
}
44+
}
Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.Actions
22
{
3-
using Base;
43
using Contracts.Actions;
54
using Contracts.Base;
6-
using Contracts.CaughtExceptions;
7-
using CaughtExceptions;
85
using Internal;
96
using Internal.Results;
107
using Internal.TestContexts;
11-
using ShouldHave;
128
using Utilities.Validators;
139

1410
/// <summary>
1511
/// Used for testing void actions.
1612
/// </summary>
17-
public class VoidActionResultTestBuilder : BaseTestBuilderWithInvokedAction, IVoidActionResultTestBuilder
13+
public class VoidActionResultTestBuilder
14+
: BaseActionResultTestBuilder<MethodResult>,
15+
IVoidActionResultTestBuilder
1816
{
1917
/// <summary>
2018
/// Initializes a new instance of the <see cref="VoidActionResultTestBuilder"/> class.
@@ -32,20 +30,5 @@ public IBaseTestBuilderWithInvokedAction ShouldReturnEmpty()
3230
InvocationValidator.CheckForException(this.CaughtException, this.TestContext.ExceptionMessagePrefix);
3331
return this.NewAndTestBuilderWithInvokedAction();
3432
}
35-
36-
/// <inheritdoc />
37-
public IShouldHaveTestBuilder<MethodResult> ShouldHave()
38-
{
39-
InvocationValidator.CheckForException(this.CaughtException, this.TestContext.ExceptionMessagePrefix);
40-
return new ShouldHaveTestBuilder<MethodResult>(this.TestContext);
41-
}
42-
43-
/// <inheritdoc />
44-
public IShouldThrowTestBuilder ShouldThrow()
45-
{
46-
TestHelper.ExecuteTestCleanup();
47-
InvocationValidator.CheckForNullException(this.CaughtException, this.TestContext.ExceptionMessagePrefix);
48-
return new ShouldThrowTestBuilder(this.TestContext);
49-
}
5033
}
5134
}

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,8 @@
77
/// Used for testing the action and its result.
88
/// </summary>
99
/// <typeparam name="TActionResult">Type of action result to be tested.</typeparam>
10-
public interface IActionResultTestBuilder<TActionResult> : IBaseTestBuilderWithInvokedAction
10+
public interface IActionResultTestBuilder<TActionResult> : IBaseActionResultTestBuilder<TActionResult>
1111
{
12-
/// <summary>
13-
/// Used for testing the action's additional data - action attributes, HTTP response, view bag and more.
14-
/// </summary>
15-
/// <returns>Test builder of <see cref="IShouldHaveTestBuilder{TActionResult}"/> type.</returns>
16-
IShouldHaveTestBuilder<TActionResult> ShouldHave();
17-
18-
/// <summary>
19-
/// Used for testing whether the action throws exception.
20-
/// </summary>
21-
/// <returns>Test builder of <see cref="IShouldThrowTestBuilder"/>.</returns>
22-
IShouldThrowTestBuilder ShouldThrow();
23-
2412
/// <summary>
2513
/// Used for testing returned action result.
2614
/// </summary>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Actions
2+
{
3+
using Base;
4+
using CaughtExceptions;
5+
6+
/// <summary>
7+
/// Base interface for action result test builders.
8+
/// </summary>
9+
/// <typeparam name="TActionResult">Type of action result to be tested.</typeparam>
10+
public interface IBaseActionResultTestBuilder<TActionResult> : IBaseTestBuilderWithInvokedAction
11+
{
12+
/// <summary>
13+
/// Used for testing the action's additional data - action attributes, HTTP response, view bag and more.
14+
/// </summary>
15+
/// <returns>Test builder of <see cref="IShouldHaveTestBuilder{TActionResult}"/> type.</returns>
16+
IShouldHaveTestBuilder<TActionResult> ShouldHave();
17+
18+
/// <summary>
19+
/// Used for testing whether the action throws exception.
20+
/// </summary>
21+
/// <returns>Test builder of <see cref="IShouldThrowTestBuilder"/>.</returns>
22+
IShouldThrowTestBuilder ShouldThrow();
23+
}
24+
}
Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,17 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Actions
22
{
33
using Base;
4-
using CaughtExceptions;
54
using Internal.Results;
65

76
/// <summary>
87
/// Used for testing void actions.
98
/// </summary>
10-
public interface IVoidActionResultTestBuilder : IBaseTestBuilderWithInvokedAction
9+
public interface IVoidActionResultTestBuilder : IBaseActionResultTestBuilder<MethodResult>
1110
{
1211
/// <summary>
1312
/// Tests whether the action result is void.
1413
/// </summary>
1514
/// <returns>Test builder of <see cref="IBaseTestBuilderWithInvokedAction"/> type.</returns>
1615
IBaseTestBuilderWithInvokedAction ShouldReturnEmpty();
17-
18-
/// <summary>
19-
/// Used for testing the action's additional data - action attributes, HTTP response, view bag and more.
20-
/// </summary>
21-
/// <returns>Test builder of <see cref="IShouldHaveTestBuilder{VoidActionResult}"/> type.</returns>
22-
IShouldHaveTestBuilder<MethodResult> ShouldHave();
23-
24-
/// <summary>
25-
/// Used for testing whether the action throws exception.
26-
/// </summary>
27-
/// <returns>Test builder of <see cref="IShouldThrowTestBuilder"/> type.</returns>
28-
IShouldThrowTestBuilder ShouldThrow();
2916
}
3017
}

src/MyTested.AspNetCore.Mvc.ModelState/Builders/Models/ModelStateTestBuilder.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ public ModelStateTestBuilder(
2323
}
2424

2525
/// <inheritdoc />
26-
public IModelErrorTestBuilder<TModel> For<TModel>()
27-
{
28-
return new ModelErrorTestBuilder<TModel>(this.TestContext, this.ModelState);
29-
}
26+
public IModelErrorTestBuilder<TModel> For<TModel>()
27+
=> new ModelErrorTestBuilder<TModel>(this.TestContext, this.ModelState);
3028
}
3129
}

src/MyTested.AspNetCore.Mvc.Pipeline/Builders/Contracts/Pipeline/IWhichControllerInstanceBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/// <summary>
88
/// Used for building the controller which will be tested after a route assertion.
99
/// </summary>
10-
/// <typeparam name="TController"></typeparam>
10+
/// <typeparam name="TController">Class representing ASP.NET Core MVC controller.</typeparam>
1111
public interface IWhichControllerInstanceBuilder<TController>
1212
: IBaseControllerBuilder<TController, IAndWhichControllerInstanceBuilder<TController>>,
1313
IActionResultTestBuilder<MethodResult>,

src/MyTested.AspNetCore.Mvc.Pipeline/Builders/Pipeline/WhichControllerInstanceBuilder.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,21 @@ public WhichControllerInstanceBuilder(ControllerTestContext testContext)
2727

2828
/// <inheritdoc />
2929
public IShouldHaveTestBuilder<MethodResult> ShouldHave()
30-
{
31-
this.InvokeAction();
30+
=> this
31+
.InvokeAndGetActionResultTestBuilder()
32+
.ShouldHave();
3233

33-
var actionResultTestBuilder = new ActionResultTestBuilder<MethodResult>(this.TestContext);
34-
35-
return actionResultTestBuilder.ShouldHave();
36-
}
34+
/// <inheritdoc />
35+
public IShouldThrowTestBuilder ShouldThrow()
36+
=> this
37+
.InvokeAndGetActionResultTestBuilder()
38+
.ShouldThrow();
3739

3840
/// <inheritdoc />
3941
public IShouldReturnTestBuilder<MethodResult> ShouldReturn()
4042
{
4143
this.InvokeAction();
42-
44+
// epa iznesi tuka IActionResultInterface che da e po-lesno iznasqneto tuk + iznesi dolu Invoke na nshto kato ActionCallExpression.Invoke()
4345
var actionResultTestBuilder = new ActionResultTestBuilder<MethodResult>(this.TestContext);
4446

4547
return actionResultTestBuilder.ShouldReturn();
@@ -56,20 +58,17 @@ public IBaseTestBuilderWithInvokedAction ShouldReturnEmpty()
5658
}
5759

5860
/// <inheritdoc />
59-
public IShouldThrowTestBuilder ShouldThrow()
61+
public IWhichControllerInstanceBuilder<TController> AndAlso() => this;
62+
63+
protected override IAndWhichControllerInstanceBuilder<TController> SetBuilder() => this;
64+
65+
private IBaseActionResultTestBuilder<MethodResult> InvokeAndGetActionResultTestBuilder()
6066
{
6167
this.InvokeAction();
6268

63-
var actionResultTestBuilder = new VoidActionResultTestBuilder(this.TestContext);
64-
65-
return actionResultTestBuilder.ShouldThrow();
69+
return new VoidActionResultTestBuilder(this.TestContext);
6670
}
6771

68-
/// <inheritdoc />
69-
public IWhichControllerInstanceBuilder<TController> AndAlso() => this;
70-
71-
protected override IAndWhichControllerInstanceBuilder<TController> SetBuilder() => this;
72-
7372
private void InvokeAction()
7473
{
7574
var methodCall = this.TestContext.MethodCall;

0 commit comments

Comments
 (0)