Skip to content

Commit 4e6e95d

Browse files
committed
Added unit tests for number of errors in the ModelState (#33)
1 parent 26cdc6c commit 4e6e95d

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/MyWebApi.Tests/BuildersTests/ActionsTests/ShouldHaveModelStateTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,33 @@ public void ShouldHaveInvalidModelStateShouldBeValidWithInvalidRequestModel()
8080
.InvalidModelState();
8181
}
8282

83+
[Test]
84+
public void ShouldHaveInvalidModelStateShouldBeValidWithInvalidRequestModelAndCorrectNumberOfErrors()
85+
{
86+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
87+
88+
MyWebApi
89+
.Controller<WebApiController>()
90+
.Calling(c => c.ModelStateCheck(requestModelWithErrors))
91+
.ShouldHave()
92+
.InvalidModelState(2);
93+
}
94+
95+
[Test]
96+
[ExpectedException(
97+
typeof(ModelErrorAssertionException),
98+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have invalid model state with 5 errors, but contained 2.")]
99+
public void ShouldHaveInvalidModelStateShouldBeInvalidWithInvalidRequestModelAndIncorrectNumberOfErrors()
100+
{
101+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
102+
103+
MyWebApi
104+
.Controller<WebApiController>()
105+
.Calling(c => c.ModelStateCheck(requestModelWithErrors))
106+
.ShouldHave()
107+
.InvalidModelState(5);
108+
}
109+
83110
[Test]
84111
[ExpectedException(
85112
typeof(ModelErrorAssertionException),
@@ -95,6 +122,21 @@ public void ShouldHaveInvalidModelStateShouldThrowExceptionWithValidRequestModel
95122
.InvalidModelState();
96123
}
97124

125+
[Test]
126+
[ExpectedException(
127+
typeof(ModelErrorAssertionException),
128+
ExpectedMessage = "When calling ModelStateCheck action in WebApiController expected to have invalid model state with 5 errors, but contained 0.")]
129+
public void ShouldHaveInvalidModelStateShouldThrowExceptionWithValidRequestModelAndProvidedNumberOfErrors()
130+
{
131+
var requestModel = TestObjectFactory.GetValidRequestModel();
132+
133+
MyWebApi
134+
.Controller<WebApiController>()
135+
.Calling(c => c.ModelStateCheck(requestModel))
136+
.ShouldHave()
137+
.InvalidModelState(5);
138+
}
139+
98140
[Test]
99141
public void AndShouldWorkCorrectlyWithValidModelState()
100142
{

src/MyWebApi/Builders/Actions/ShouldHave/ShouldHaveModelState.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ public IAndTestBuilder<TActionResult> ValidModelState()
5656
public IAndTestBuilder<TActionResult> InvalidModelState(int? numberOfErrors = null)
5757
{
5858
var actualModelStateErrors = this.Controller.ModelState.Count;
59-
if (actualModelStateErrors == (numberOfErrors ?? 0))
59+
if (actualModelStateErrors == 0
60+
|| (numberOfErrors != null && actualModelStateErrors != numberOfErrors))
6061
{
6162
throw new ModelErrorAssertionException(string.Format(
6263
"When calling {0} action in {1} expected to have invalid model state{2}, {3}.",
6364
this.ActionName,
6465
this.Controller.GetName(),
6566
numberOfErrors == null ? string.Empty : string.Format(" with {0} errors", numberOfErrors),
66-
numberOfErrors == null ? "but was in fact valid" : string.Format(" but contained {0}", actualModelStateErrors)));
67+
numberOfErrors == null ? "but was in fact valid" : string.Format("but contained {0}", actualModelStateErrors)));
6768
}
6869

6970
return this.NewAndTestBuilder();

0 commit comments

Comments
 (0)