-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add assert against ModelStateDictionary instances having errors
- Expect(modelState){.Not}.To.Have.Errors();
- Loading branch information
1 parent
df5953c
commit ac7302a
Showing
6 changed files
with
197 additions
and
8 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/NExpect.Matchers.AspNetCore.Tests/TestModelStateMatchers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using NExpect.Exceptions; | ||
|
||
namespace NExpect.Matchers.AspNet.Tests; | ||
|
||
[TestFixture] | ||
public class TestModelStateMatchers | ||
{ | ||
[TestFixture] | ||
public class Errors | ||
{ | ||
[TestFixture] | ||
public class WhenModelStateHasErrors | ||
{ | ||
[Test] | ||
public void ShouldReturnAssertErrorsExist() | ||
{ | ||
// Arrange | ||
var modelState = new ModelStateDictionary(); | ||
modelState.AddModelError(GetRandomString(), GetRandomWords()); | ||
// Act | ||
Assert.That( | ||
() => | ||
{ | ||
Expect(modelState) | ||
.To.Have.Errors(); | ||
}, | ||
Throws.Nothing | ||
); | ||
|
||
// Assert | ||
} | ||
|
||
[Test] | ||
public void ShouldFailIfNegatedAndErrorsExist() | ||
{ | ||
// Arrange | ||
var modelState = new ModelStateDictionary(); | ||
modelState.AddModelError(GetRandomString(), GetRandomWords()); | ||
|
||
// Act | ||
Assert.That( | ||
() => | ||
{ | ||
Expect(modelState) | ||
.Not.To.Have.Errors(); | ||
}, | ||
Throws.Exception.InstanceOf<UnmetExpectationException>() | ||
); | ||
// Assert | ||
} | ||
} | ||
|
||
[TestFixture] | ||
public class WHenModelStateHasNoErrors | ||
{ | ||
[Test] | ||
public void ShouldAssertNoErrors() | ||
{ | ||
// Arrange | ||
var modelState = new ModelStateDictionary(); | ||
// Act | ||
Assert.That( | ||
() => | ||
{ | ||
Expect(modelState) | ||
.Not.To.Have.Errors(); | ||
}, | ||
Throws.Nothing | ||
); | ||
Assert.That( | ||
() => | ||
{ | ||
Expect(modelState) | ||
.To.Have.Errors(); | ||
}, | ||
Throws.Exception.InstanceOf<UnmetExpectationException>() | ||
); | ||
// Assert | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Imported.PeanutButter.Utils; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using NExpect.Implementations; | ||
using NExpect.Interfaces; | ||
using NExpect.MatcherLogic; | ||
using static NExpect.Implementations.MessageHelpers; | ||
|
||
namespace NExpect; | ||
|
||
/// <summary> | ||
/// Provides matchers for ModelStateDictionary objects | ||
/// </summary> | ||
public static class ModelStateMatchers | ||
{ | ||
/// <summary> | ||
/// Asserts that the model state dictionary has errors | ||
/// </summary> | ||
/// <param name="have"></param> | ||
/// <returns></returns> | ||
public static ICollectionMore<KeyValuePair<string, ModelStateEntry>> Errors( | ||
this ICollectionHave<KeyValuePair<string, ModelStateEntry>> have | ||
) | ||
{ | ||
return have.Errors(NULL_STRING); | ||
} | ||
|
||
/// <summary> | ||
/// Asserts that the model state dictionary has errors | ||
/// </summary> | ||
/// <param name="have"></param> | ||
/// <param name="customMessage"></param> | ||
/// <returns></returns> | ||
public static ICollectionMore<KeyValuePair<string, ModelStateEntry>> Errors( | ||
this ICollectionHave<KeyValuePair<string, ModelStateEntry>> have, | ||
string customMessage | ||
) | ||
{ | ||
return have.Errors(() => customMessage); | ||
} | ||
|
||
/// <summary> | ||
/// Asserts that the model state dictionary has errors | ||
/// </summary> | ||
/// <param name="have"></param> | ||
/// <param name="customMessageGenerator"></param> | ||
/// <returns></returns> | ||
public static ICollectionMore<KeyValuePair<string, ModelStateEntry>> Errors( | ||
this ICollectionHave<KeyValuePair<string, ModelStateEntry>> have, | ||
Func<string> customMessageGenerator | ||
) | ||
{ | ||
return have.AddMatcher( | ||
collection => | ||
{ | ||
if ( | ||
!collection.TryGetMetadata<ModelStateDictionary>("__actual__", out var actual) || | ||
actual is null | ||
) | ||
{ | ||
return new EnforcedMatcherResult( | ||
false, | ||
FinalMessageFor( | ||
() => "Unable to assert model state errors on a null model state dictionary", | ||
customMessageGenerator | ||
) | ||
); | ||
} | ||
|
||
var passed = actual.ErrorCount > 0; | ||
return new MatcherResult( | ||
passed, | ||
FinalMessageFor( | ||
() => $"Expected {passed.AsNot()}to find errors (found {actual.ErrorCount})", | ||
customMessageGenerator | ||
) | ||
); | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters