-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathBadRequestObjectResultAssertions_Tests.cs
72 lines (59 loc) · 2.65 KB
/
BadRequestObjectResultAssertions_Tests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using FluentAssertions.Mvc.Tests.Helpers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System;
using Xunit;
namespace FluentAssertions.AspNetCore.Mvc.Tests
{
public class BadRequestObjectResultAssertions_Tests
{
private const string TestError = "testError";
[Fact]
public void Error_GivenBadRequestObjectResult_ShouldHaveTheSameError()
{
var result = new TestController().BadRequest(TestError);
result.Should().BeBadRequestObjectResult().Error.Should().BeSameAs(TestError);
}
[Fact]
public void SerializableError_GivenExpectedModelState_ShouldPass()
{
const string testErrorKey = "TestErrorKey";
const string testErrorMessage = "TestErrorMessage";
var testModelState = new ModelStateDictionary();
testModelState.AddModelError(testErrorKey, testErrorMessage);
var result = new TestController().BadRequest(testModelState);
result.Should().BeBadRequestObjectResult().SerializableError.Should().ContainKey(testErrorKey);
}
[Fact]
public void ErrorAs_GivenExpectedError_ShouldPass()
{
var result = new TestController().BadRequest(TestError);
result.Should().BeBadRequestObjectResult().ErrorAs<string>().Should().Be(TestError);
}
[Fact]
public void ErrorAs_GivenUnexpectedError_ShouldFail()
{
var result = new TestController().BadRequest(TestError);
Action a = () => result.Should().BeBadRequestObjectResult().ErrorAs<string>().Should().Be("xyx");
a.Should().Throw<Exception>();
}
[Fact]
public void ErrorAs_GivenWrongType_ShouldFail()
{
var result = new TestController().BadRequest(TestError);
var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY(
"BadRequestObjectResult.Error", typeof(int), typeof(string));
Action a = () => result.Should().BeBadRequestObjectResult().ErrorAs<int>().Should().Be(2);
a.Should().Throw<Exception>().WithMessage(failureMessage);
}
[Fact]
public void ErrorAs_Null_ShouldFail()
{
ActionResult result = new BadRequestObjectResult(null as object);
var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundNull(
"BadRequestObjectResult.Error", typeof(object));
Action a = () => result.Should().BeBadRequestObjectResult().ErrorAs<object>();
a.Should().Throw<Exception>().WithMessage(failureMessage);
}
}
}