Created
January 26, 2016 22:26
-
-
Save plioi/2cb4b4255cd1b301b4c3 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Linq; | |
using MediatR; | |
using Should; | |
using static Testing; | |
public static class Assertions | |
{ | |
public static void ShouldValidate<TResponse>(this IRequest<TResponse> message) | |
{ | |
var validator = Validator(message); | |
validator.ShouldNotBeNull($"There is no validator for {message.GetType()} messages"); | |
var result = validator.Validate(message); | |
var indentedErrorMessages = result | |
.Errors | |
.OrderBy(x => x.ErrorMessage) | |
.Select(x => " " + x.ErrorMessage) | |
.ToArray(); | |
var actual = String.Join(Environment.NewLine, indentedErrorMessages); | |
result.IsValid.ShouldBeTrue( | |
$"Expected no validation errors, but found {result.Errors.Count}:{Environment.NewLine}{actual}"); | |
} | |
public static void ShouldNotValidate<TResponse>(this IRequest<TResponse> message, params string[] expectedErrors) | |
{ | |
var validator = Validator(message); | |
validator.ShouldNotBeNull($"There is no validator for {message.GetType()} messages"); | |
var result = validator.Validate(message); | |
result.IsValid.ShouldBeFalse("Expected validation errors, but the message passed validation."); | |
var actual = result.Errors | |
.OrderBy(x => x.ErrorMessage) | |
.Select(x => x.ErrorMessage) | |
.ToArray(); | |
actual.ShouldEqual(expectedErrors.OrderBy(x => x).ToArray()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment