Skip to content

Commit 2f58498

Browse files
committed
Merge pull request #39 from ivaylokenov/and-but-and-also-chaining-methods
And but and also chaining methods
2 parents e6d7d18 + 03b4ac0 commit 2f58498

36 files changed

+253
-94
lines changed

MyWebApi.Tests/BuildersTests/ActionsTests/ShouldHaveModelStateTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
namespace MyWebApi.Tests.BuildersTests.ActionsTests
22
{
33
using Exceptions;
4+
45
using NUnit.Framework;
6+
57
using Setups;
68
using Setups.Models;
79

@@ -71,5 +73,31 @@ public void ShouldHaveInvalidModelStateShouldThrowExceptionWithValidRequestModel
7173
.Calling(c => c.ModelStateCheck(requestModel))
7274
.ShouldHaveInvalidModelState();
7375
}
76+
77+
[Test]
78+
public void AndShouldWorkCorrectlyWithValidModelState()
79+
{
80+
var requestModel = TestObjectFactory.GetValidRequestModel();
81+
82+
MyWebApi
83+
.Controller<WebApiController>()
84+
.Calling(c => c.ModelStateCheck(requestModel))
85+
.ShouldHaveValidModelState()
86+
.AndAlso()
87+
.ShouldReturnOk();
88+
}
89+
90+
[Test]
91+
public void AndShouldWorkCorrectlyWithInvalidModelState()
92+
{
93+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
94+
95+
MyWebApi
96+
.Controller<WebApiController>()
97+
.Calling(c => c.ModelStateCheck(requestModelWithErrors))
98+
.ShouldHaveInvalidModelState()
99+
.AndAlso()
100+
.ShouldReturnOk();
101+
}
74102
}
75103
}

MyWebApi.Tests/BuildersTests/BadRequestsTests/BadRequestTestBuilderTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,13 @@ public void WithModelStateForShouldNotThrowExceptionWhenModelStateHasSameErrors(
175175
.ShouldReturnBadRequest()
176176
.WithModelStateFor<RequestModel>()
177177
.ContainingModelStateErrorFor(m => m.Integer).ThatEquals("The field Integer must be between 1 and 2147483647.")
178-
.And()
178+
.AndAlso()
179179
.ContainingModelStateErrorFor(m => m.RequiredString).BeginningWith("The RequiredString")
180-
.And()
180+
.AndAlso()
181181
.ContainingModelStateErrorFor(m => m.RequiredString).EndingWith("required.")
182-
.And()
182+
.AndAlso()
183183
.ContainingModelStateErrorFor(m => m.RequiredString).Containing("field")
184-
.And()
184+
.AndAlso()
185185
.ContainingNoModelStateErrorFor(m => m.NonRequiredString);
186186
}
187187
}

MyWebApi.Tests/BuildersTests/ControllerBuilderTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Web.Http.Results;
77

88
using Builders.Contracts;
9+
using Builders.Contracts.Actions;
910
using Exceptions;
1011
using NUnit.Framework;
1112
using Setups;

MyWebApi.Tests/BuildersTests/ModelsTests/ModelErrorDetailsTestBuilderTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public void ThatEqualsShouldNotThrowExceptionWhenProvidedMessageIsValid()
2121
.ShouldHaveModelStateFor<RequestModel>()
2222
.ContainingNoModelStateErrorFor(m => m.NonRequiredString)
2323
.ContainingModelStateErrorFor(m => m.RequiredString).ThatEquals("The RequiredString field is required.")
24-
.And()
24+
.AndAlso()
2525
.ContainingModelStateErrorFor(m => m.RequiredString)
26-
.And()
26+
.AndAlso()
2727
.ContainingNoModelStateErrorFor(m => m.NotValidateInteger)
28-
.And()
28+
.AndAlso()
2929
.ContainingModelStateError("RequiredString")
3030
.ContainingModelStateErrorFor(m => m.Integer).ThatEquals(string.Format("The field Integer must be between {0} and {1}.", 1, int.MaxValue))
3131
.ContainingModelStateError("RequiredString")
@@ -46,7 +46,7 @@ public void ThatEqualsShouldThrowExceptionWhenProvidedMessageIsValid()
4646
.Calling(c => c.ModelStateCheck(requestModelWithErrors))
4747
.ShouldHaveModelStateFor<RequestModel>()
4848
.ContainingNoModelStateErrorFor(m => m.NonRequiredString)
49-
.And()
49+
.AndAlso()
5050
.ContainingModelStateErrorFor(m => m.RequiredString).ThatEquals("RequiredString field is required.")
5151
.ContainingModelStateErrorFor(m => m.Integer).ThatEquals(string.Format("Integer must be between {0} and {1}.", 1, int.MaxValue));
5252
}

MyWebApi.Tests/BuildersTests/UnauthorizedTests/AndUnauthorizedTestBuilderTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void AndShouldReturnCorrectResultsWhenHeadersAreCorrect()
1717
.Calling(c => c.UnauthorizedActionWithChallenges())
1818
.ShouldReturnUnauthorized()
1919
.ContainingAuthenticationHeaderChallenge("TestScheme", "TestParameter")
20-
.And()
20+
.AndAlso()
2121
.ContainingAuthenticationHeaderChallenge("Basic");
2222
}
2323

@@ -32,7 +32,7 @@ public void AndShouldThrowExceptionWhenHeadersAreInCorrect()
3232
.Calling(c => c.UnauthorizedActionWithChallenges())
3333
.ShouldReturnUnauthorized()
3434
.ContainingAuthenticationHeaderChallenge("TestScheme", "TestParameter")
35-
.And()
35+
.AndAlso()
3636
.ContainingAuthenticationHeaderChallenge("Scheme", "Parameter");
3737
}
3838
}

MyWebApi.Tests/BuildersTests/UnauthorizedTests/UnauthorizedResultTestBuilderTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,9 @@ public void WithAuthenticationHeaderChallengesShouldNotThrowExceptionWhenResultC
312312
authHeaders =>
313313
authHeaders
314314
.ContainingHeader(header => header.WithScheme("Basic"))
315-
.And()
315+
.AndAlso()
316316
.ContainingHeader(header => header.WithScheme("TestScheme").WithParameter("TestParameter"))
317-
.And()
317+
.AndAlso()
318318
.ContainingHeader(header => header.WithScheme("YetAnotherScheme").WithParameter("YetAnotherParameter")));
319319
}
320320

@@ -329,9 +329,9 @@ public void WithAuthenticationHeaderChallengesShouldNotThrowExceptionWhenResultC
329329
authHeaders =>
330330
authHeaders
331331
.ContainingHeader(header => header.WithScheme("TestScheme").WithParameter("TestParameter"))
332-
.And()
332+
.AndAlso()
333333
.ContainingHeader(header => header.WithScheme("YetAnotherScheme").WithParameter("YetAnotherParameter"))
334-
.And()
334+
.AndAlso()
335335
.ContainingHeader(header => header.WithScheme("Basic")));
336336
}
337337

@@ -349,7 +349,7 @@ public void WithAuthenticationHeaderChallengesShouldThrowExceptionWhenResultDoes
349349
authHeaders =>
350350
authHeaders
351351
.ContainingHeader(header => header.WithScheme("TestScheme").WithParameter("TestParameter"))
352-
.And()
352+
.AndAlso()
353353
.ContainingHeader(header => header.WithScheme("Basic")));
354354
}
355355

@@ -367,9 +367,9 @@ public void WithAuthenticationHeaderChallengesShouldThrowExceptionWhenResultDoes
367367
authHeaders =>
368368
authHeaders
369369
.ContainingHeader(header => header.WithScheme("TestScheme").WithParameter("TestParameter"))
370-
.And()
370+
.AndAlso()
371371
.ContainingHeader(header => header.WithScheme("YetAnotherScheme").WithParameter("YetAnotherParameter"))
372-
.And()
372+
.AndAlso()
373373
.ContainingHeader(header => header.WithScheme("Scheme")));
374374
}
375375
}

MyWebApi.Tests/MyWebApiTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace MyWebApi.Tests
22
{
33
using NUnit.Framework;
4+
45
using Setups;
56
using Setups.Services;
67

@@ -27,5 +28,15 @@ public void ControllerWithConstructorFunctionShouldPopulateCorrectNewInstanceOfC
2728
Assert.IsNotNull(controller.InjectedService);
2829
Assert.IsAssignableFrom<InjectedService>(controller.InjectedService);
2930
}
31+
32+
[Test]
33+
public void ControllerWithProvidedInstanceShouldPopulateCorrectInstanceOfControllerType()
34+
{
35+
var instance = new WebApiController();
36+
var controller = MyWebApi.Controller(instance).Controller;
37+
38+
Assert.IsNotNull(controller);
39+
Assert.IsAssignableFrom<WebApiController>(controller);
40+
}
3041
}
3142
}

MyWebApi/Builders/Actions/ActionResultTestBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
using Base;
77
using Common.Extensions;
8-
using Contracts;
8+
using Contracts.Actions;
99
using Exceptions;
1010
using Utilities;
1111

1212
/// <summary>
1313
/// Used for testing the action result type of test.
1414
/// </summary>
1515
/// <typeparam name="TActionResult">Result from invoked action in ASP.NET Web API controller.</typeparam>
16-
public partial class ActionResultTestBuilder<TActionResult>
16+
public partial class ActionResultTestBuilder<TActionResult>
1717
: BaseTestBuilderWithActionResult<TActionResult>, IActionResultTestBuilder<TActionResult>
1818
{
1919
/// <summary>

MyWebApi/Builders/Actions/ShouldHaveModelState.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace MyWebApi.Builders.Actions
22
{
33
using Common.Extensions;
4+
using Contracts.And;
45
using Contracts.Models;
56
using Exceptions;
67
using Models;
@@ -24,15 +25,18 @@ public IModelErrorTestBuilder<TRequestModel> ShouldHaveModelStateFor<TRequestMod
2425
/// <summary>
2526
/// Checks whether the tested action's provided model state is valid.
2627
/// </summary>
27-
public void ShouldHaveValidModelState()
28+
/// <returns>Test builder with AndAlso method.</returns>
29+
public IAndTestBuilder<TActionResult> ShouldHaveValidModelState()
2830
{
2931
this.CheckValidModelState();
32+
return this.NewAndTestBuilder();
3033
}
3134

3235
/// <summary>
3336
/// Checks whether the tested action's provided model state is not valid.
3437
/// </summary>
35-
public void ShouldHaveInvalidModelState()
38+
/// <returns>Test builder with AndAlso method.</returns>
39+
public IAndTestBuilder<TActionResult> ShouldHaveInvalidModelState()
3640
{
3741
if (this.Controller.ModelState.Count == 0)
3842
{
@@ -41,6 +45,8 @@ public void ShouldHaveInvalidModelState()
4145
this.ActionName,
4246
this.Controller.GetName()));
4347
}
48+
49+
return this.NewAndTestBuilder();
4450
}
4551
}
4652
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace MyWebApi.Builders.And
2+
{
3+
using System.Web.Http;
4+
using Actions;
5+
using Base;
6+
using Contracts.Actions;
7+
using Contracts.And;
8+
9+
/// <summary>
10+
/// Class containing AndAlso() method allowing additional assertions after model state tests.
11+
/// </summary>
12+
/// <typeparam name="TActionResult">Result from invoked action in ASP.NET Web API controller.</typeparam>
13+
public class AndTestBuilder<TActionResult> : BaseTestBuilderWithActionResult<TActionResult>,
14+
IAndTestBuilder<TActionResult>
15+
{
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="AndTestBuilder{TActionResult}" /> class.
18+
/// </summary>
19+
/// <param name="controller">Controller on which the action will be tested.</param>
20+
/// <param name="actionName">Name of the tested action.</param>
21+
/// <param name="actionResult">Result from the tested action.</param>
22+
public AndTestBuilder(ApiController controller, string actionName, TActionResult actionResult)
23+
: base(controller, actionName, actionResult)
24+
{
25+
}
26+
27+
/// <summary>
28+
/// Method allowing additional assertions after the model state tests.
29+
/// </summary>
30+
/// <returns>Builder for testing the action result.</returns>
31+
public IActionResultTestBuilder<TActionResult> AndAlso()
32+
{
33+
return new ActionResultTestBuilder<TActionResult>(this.Controller, this.ActionName, this.ActionResult);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)