-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCreatedAtActionResultAssertions_Tests.cs
125 lines (99 loc) · 5.31 KB
/
CreatedAtActionResultAssertions_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using FluentAssertions.Mvc.Tests.Helpers;
using Microsoft.AspNetCore.Mvc;
using System;
using Xunit;
namespace FluentAssertions.AspNetCore.Mvc.Tests
{
public class CreatedAtActionResultAssertions_Tests
{
public const string Reason = FailureMessageHelper.Reason;
public readonly static object[] ReasonArgs = FailureMessageHelper.ReasonArgs;
private const string TestValue = "testValue";
[Fact]
public void WithActionName_GivenExpectedActionName_ShouldPass()
{
var expectedActionName = "expectedAction";
var result = new CreatedAtActionResult(expectedActionName, string.Empty, null, null);
result.Should().BeCreatedAtActionResult().WithActionName(expectedActionName);
}
[Fact]
public void WithActionName_GivenUnexpectedActionName_ShouldFail()
{
var result = new CreatedAtActionResult("someOtherAction", string.Empty, null, null);
string failureMessage = FailureMessageHelper.ExpectedContextToBeXButY(
"CreatedAtActionResult.ActionName", "expectedAction", "someOtherAction");
Action a = () => result.Should().BeCreatedAtActionResult().WithActionName("expectedAction", Reason, ReasonArgs);
a.Should().Throw<Exception>().WithMessage(
failureMessage);
}
[Fact]
public void WithControllerName_GivenExpectedControllerName_ShouldPass()
{
var expectedControllerName = "expectedController";
var result = new CreatedAtActionResult(string.Empty, expectedControllerName, null, null);
result.Should().BeCreatedAtActionResult().WithControllerName(expectedControllerName);
}
[Fact]
public void WithControllerName_GivenUnexpectedControllerName_ShouldFail()
{
var result = new CreatedAtActionResult(string.Empty, "someOtherController", null, null);
string failureMessage = FailureMessageHelper.ExpectedContextToBeXButY(
"CreatedAtActionResult.ControllerName", "expectedController", "someOtherController");
Action a = () => result.Should().BeCreatedAtActionResult().WithControllerName("expectedController", Reason, ReasonArgs);
a.Should().Throw<Exception>().WithMessage(failureMessage);
}
[Fact]
public void WithRouteValue_GivenKeyDoesntExist_ShouldFail()
{
var expectedKey = "expectedKey";
var routeValues = new { myKey = "MyValue" };
var result = new CreatedAtActionResult(string.Empty, string.Empty, routeValues, null);
string failureMessage = FailureMessageHelper.ExpectedContextContainValueAtKeyButKeyNotFound(
"CreatedAtActionResult.RouteValues", "Val", expectedKey);
Action a = () => result.Should().BeCreatedAtActionResult().WithRouteValue(expectedKey, "Val", Reason, ReasonArgs);
a.Should().Throw<Exception>().WithMessage(failureMessage);
}
[Fact]
public void WithRouteValue_GivenExpectedKeyValuePair_ShouldPass()
{
var expectedKey = "expectedKey";
var expectedValue = "expectedValue";
var routeValues = new { expectedKey = expectedValue };
var result = new CreatedAtActionResult(string.Empty, string.Empty, routeValues, null);
result.Should().BeCreatedAtActionResult().WithRouteValue(expectedKey, expectedValue);
}
[Fact]
public void WithRouteValue_GivenUnexpectedValue_ShouldFail()
{
var expectedKey = "expectedKey";
var expectedValue = "expectedValue";
var routeValues = new { expectedKey = "someOtherValue" };
var result = new CreatedAtActionResult(string.Empty, string.Empty, routeValues, null);
string failureMessage = FailureMessageHelper.ExpectedAtKeyValueXButFoundY(
"CreatedAtActionResult.RouteValues", expectedKey, expectedValue, "someOtherValue");
Action a = () => result.Should().BeCreatedAtActionResult().WithRouteValue(expectedKey, expectedValue, Reason, ReasonArgs);
a.Should().Throw<Exception>().WithMessage(failureMessage);
}
[Fact]
public void ValueAs_GivenCreatedAtActionResult_ShouldHaveTheSameValue()
{
var result = new TestController().CreatedAtAction(string.Empty, string.Empty, null, TestValue);
result.Should().BeCreatedAtActionResult().ValueAs<string>().Should().BeSameAs(TestValue);
}
[Fact]
public void ValueAs_GivenWrongType_ShouldFail()
{
var result = new TestController().CreatedAtAction(string.Empty, string.Empty, null, TestValue);
Action a = () => result.Should().BeCreatedAtActionResult().ValueAs<int>().Should().Be(2);
a.Should().Throw<Exception>();
}
[Fact]
public void ValueAs_Null_ShouldFail()
{
ActionResult result = new CreatedAtActionResult(string.Empty, string.Empty, null, null);
var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundNull("CreatedAtActionResult.Value", typeof(object));
Action a = () => result.Should().BeCreatedAtActionResult().ValueAs<object>();
a.Should().Throw<Exception>().WithMessage(failureMessage);
}
}
}