Skip to content

Commit 1f59bcb

Browse files
committed
Bad request model state test builder now uses the same style as the other model state builders (closes #189)
1 parent dbbeaa8 commit 1f59bcb

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

src/MyTested.AspNetCore.Mvc.DataAnnotations/BadRequestTestBuilderDataAnnotationsExtensions.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace MyTested.AspNetCore.Mvc
22
{
3+
using System;
34
using System.Collections.Generic;
45
using System.Linq;
56
using Builders.ActionResults.BadRequest;
@@ -101,18 +102,24 @@ public static IAndBadRequestTestBuilder WithModelStateError(
101102
/// <summary>
102103
/// Tests whether <see cref="BadRequestObjectResult"/> contains specific model state errors using test builder.
103104
/// </summary>
104-
/// <typeparam name="TRequestModel">Type of model for which the model state errors will be tested.</typeparam>
105105
/// <param name="badRequestTestBuilder">Instance of <see cref="IBadRequestTestBuilder"/> type.</param>
106+
/// <param name="modelStateTestBuilder">Model state errors test builder.</param>
106107
/// <returns>The same <see cref="IAndBadRequestTestBuilder"/>.</returns>
107-
public static IModelErrorTestBuilder<TRequestModel> WithModelStateErrorFor<TRequestModel>(this IBadRequestTestBuilder badRequestTestBuilder)
108+
public static IAndBadRequestTestBuilder WithModelStateError(
109+
this IBadRequestTestBuilder badRequestTestBuilder,
110+
Action<IModelStateTestBuilder> modelStateTestBuilder)
108111
{
109112
var actualBadRequestTestBuilder = GetBadRequestTestBuilder(badRequestTestBuilder);
110113

111114
actualBadRequestTestBuilder.TestContext.Model = actualBadRequestTestBuilder.GetBadRequestObjectResultValue();
112115

113-
return new ModelErrorTestBuilder<TRequestModel>(
116+
var newModelStateTestBuilder = new ModelStateTestBuilder(
114117
actualBadRequestTestBuilder.TestContext,
115118
modelState: actualBadRequestTestBuilder.GetModelStateFromSerializableError(actualBadRequestTestBuilder.TestContext.Model));
119+
120+
modelStateTestBuilder(newModelStateTestBuilder);
121+
122+
return actualBadRequestTestBuilder;
116123
}
117124

118125
private static BadRequestTestBuilder<BadRequestObjectResult> GetBadRequestTestBuilder(IBadRequestTestBuilder badRequestTestBuilder)
Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.ActionResultsTests.BadRequestTests
22
{
3+
using Exceptions;
34
using Microsoft.AspNetCore.Mvc.ModelBinding;
45
using Setups;
56
using Setups.Controllers;
67
using Setups.Models;
78
using Xunit;
8-
9+
910
public class BadRequestTestBuilderTests
1011
{
1112
[Fact]
@@ -21,16 +22,48 @@ public void WithModelStateForShouldNotThrowExceptionWhenModelStateHasSameErrors(
2122
.Calling(c => c.BadRequestWithModelState(requestModelWithErrors))
2223
.ShouldReturn()
2324
.BadRequest()
24-
.WithModelStateErrorFor<RequestModel>()
25-
.ContainingErrorFor(m => m.Integer).ThatEquals("The field Integer must be between 1 and 2147483647.")
26-
.AndAlso()
27-
.ContainingErrorFor(m => m.RequiredString).BeginningWith("The RequiredString")
28-
.AndAlso()
29-
.ContainingErrorFor(m => m.RequiredString).EndingWith("required.")
30-
.AndAlso()
31-
.ContainingErrorFor(m => m.RequiredString).Containing("field")
32-
.AndAlso()
33-
.ContainingNoErrorFor(m => m.NonRequiredString);
25+
.WithModelStateError(modelStateError => modelStateError
26+
.For<RequestModel>()
27+
.ContainingErrorFor(m => m.Integer).ThatEquals("The field Integer must be between 1 and 2147483647.")
28+
.AndAlso()
29+
.ContainingErrorFor(m => m.RequiredString).BeginningWith("The RequiredString")
30+
.AndAlso()
31+
.ContainingErrorFor(m => m.RequiredString).EndingWith("required.")
32+
.AndAlso()
33+
.ContainingErrorFor(m => m.RequiredString).Containing("field")
34+
.AndAlso()
35+
.ContainingNoErrorFor(m => m.NonRequiredString));
36+
}
37+
38+
[Fact]
39+
public void WithModelStateForShouldThrowExceptionWhenModelStateHasSameErrors()
40+
{
41+
Test.AssertException<ModelErrorAssertionException>(
42+
() =>
43+
{
44+
var requestModelWithErrors = TestObjectFactory.GetRequestModelWithErrors();
45+
var modelState = new ModelStateDictionary();
46+
modelState.AddModelError("Integer", "The field Integer must be between 1 and 2147483647.");
47+
modelState.AddModelError("RequiredString", "The RequiredString field is required.");
48+
49+
MyController<MvcController>
50+
.Instance()
51+
.Calling(c => c.BadRequestWithModelState(requestModelWithErrors))
52+
.ShouldReturn()
53+
.BadRequest()
54+
.WithModelStateError(modelStateError => modelStateError
55+
.For<RequestModel>()
56+
.ContainingErrorFor(m => m.Integer).ThatEquals("The field Integer 1 must be between 1 and 2147483647.")
57+
.AndAlso()
58+
.ContainingErrorFor(m => m.RequiredString).BeginningWith("The RequiredString")
59+
.AndAlso()
60+
.ContainingErrorFor(m => m.RequiredString).EndingWith("required.")
61+
.AndAlso()
62+
.ContainingErrorFor(m => m.RequiredString).Containing("field")
63+
.AndAlso()
64+
.ContainingNoErrorFor(m => m.NonRequiredString));
65+
},
66+
"When calling BadRequestWithModelState action in MvcController expected error message for key Integer to be 'The field Integer 1 must be between 1 and 2147483647.', but instead found 'The field Integer must be between 1 and 2147483647.'.");
3467
}
3568
}
3669
}

0 commit comments

Comments
 (0)