Skip to content

Commit 2fa8b17

Browse files
committed
Fix LocalRedirectObjectResultAssertions and JsonResultAssertions Tests.
1 parent 06f8468 commit 2fa8b17

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

src/FluentAssertions.AspNetCore.Mvc/LocalRedirectResultAssertions.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public LocalRedirectResultAssertions WithLocalUrl(string expectedLocalUrl, strin
5454
Execute.Assertion
5555
.ForCondition(string.Equals(actualLocalUrl, expectedLocalUrl))
5656
.BecauseOf(reason, reasonArgs)
57-
.FailWith(string.Format(FailureMessages.CommonFailMessage, "LocalRedirectResult.LocalUrl", expectedLocalUrl, actualLocalUrl));
57+
.WithDefaultIdentifier("LocalRedirectResult.LocalUrl")
58+
.FailWith(FailureMessages.CommonFailMessage, expectedLocalUrl, actualLocalUrl);
5859

5960
return this;
6061
}
@@ -77,7 +78,8 @@ public LocalRedirectResultAssertions WithPermanent(bool expectedPermanent, strin
7778
Execute.Assertion
7879
.ForCondition(expectedPermanent == actualPermanent)
7980
.BecauseOf(reason, reasonArgs)
80-
.FailWith("Expected LocalRedirectResult.Permanent to be {0}{reason} but was {1}", expectedPermanent, actualPermanent);
81+
.WithDefaultIdentifier("LocalRedirectResult.Permanent")
82+
.FailWith(FailureMessages.CommonFailMessage, expectedPermanent, actualPermanent);
8183

8284
return this;
8385
}
@@ -100,7 +102,8 @@ public LocalRedirectResultAssertions WithPreserveMethod(bool expectedPreserveMet
100102
Execute.Assertion
101103
.ForCondition(expectedPreserveMethod == actualPreserveMethod)
102104
.BecauseOf(reason, reasonArgs)
103-
.FailWith("Expected LocalRedirectResult.PreserveMethod to be {0}{reason} but was {1}", expectedPreserveMethod, actualPreserveMethod);
105+
.WithDefaultIdentifier("LocalRedirectResult.PreserveMethod")
106+
.FailWith(FailureMessages.CommonFailMessage, expectedPreserveMethod, actualPreserveMethod);
104107

105108
return this;
106109
}

tests/FluentAssertions.AspNetCore.Mvc.Tests/JsonResultAssertions_Tests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public void ValueAs_GivenJsonResultWithValue_ShouldReturnTheSame()
8686
public void ValueAs_GivenWrongType_ShouldFail()
8787
{
8888
var result = new TestController().JsonSimpleValue();
89-
const string failureMessage = "Expected Value to be of type System.Int32 but was System.String.";
89+
string failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY(
90+
"JsonResult.Value", typeof(int).FullName, typeof(string).FullName);
9091

9192
Action a = () => result.Should().BeJsonResult().ValueAs<int>().Should().Be(2);
9293
a.Should().Throw<Exception>()
@@ -97,9 +98,10 @@ public void ValueAs_GivenWrongType_ShouldFail()
9798
public void ValueAs_Null_ShouldFail()
9899
{
99100
ActionResult result = new JsonResult(null);
100-
string failureMessage = $"Expected JsonResult.Value to be of type System.Object, but no value was supplied.";
101+
string failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundNull(
102+
"JsonResult.Value", typeof(object).FullName);
101103

102-
Action a = () => result.Should().BeJsonResult().ValueAs<Object>();
104+
Action a = () => result.Should().BeJsonResult().ValueAs<object>();
103105

104106
a.Should().Throw<Exception>()
105107
.WithMessage(failureMessage);

tests/FluentAssertions.AspNetCore.Mvc.Tests/LocalRedirectResultAssertions_Tests.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
using System;
2+
using FluentAssertions.Mvc.Tests.Helpers;
23
using Microsoft.AspNetCore.Mvc;
34
using Xunit;
45

56
namespace FluentAssertions.AspNetCore.Mvc.Tests
67
{
78
public class LocalRedirectObjectResultAssertions_Tests
89
{
10+
public const string Reason = FailureMessageHelper.Reason;
11+
public readonly static object[] ReasonArgs = FailureMessageHelper.ReasonArgs;
912
private const string TestLocalUrl = "localUrl";
1013

1114
[Fact]
1215
public void WithLocalUrl_GivenExpectedLocalUrl_ShouldPass()
1316
{
1417
var result = new TestController().LocalRedirect(TestLocalUrl);
18+
1519
result.Should().BeLocalRedirectResult().WithLocalUrl(TestLocalUrl);
1620
}
1721

@@ -21,16 +25,18 @@ public void WithLocalUrl_GivenUnexpectedLocalUrl_ShouldFail()
2125
const string actualLocalUrl = TestLocalUrl;
2226
const string expectedLocalUrl = "otherUrl";
2327
ActionResult result = new LocalRedirectResult(TestLocalUrl);
28+
var failureMessage = FailureMessageHelper.ExpectedContextToBeXButY("LocalRedirectResult.LocalUrl", expectedLocalUrl, actualLocalUrl);
29+
30+
Action a = () => result.Should().BeLocalRedirectResult().WithLocalUrl(expectedLocalUrl, Reason, ReasonArgs);
2431

25-
var failureMessage = string.Format(FailureMessages.CommonFailMessage, "LocalRedirectResult.LocalUrl", expectedLocalUrl, actualLocalUrl);
26-
Action a = () => result.Should().BeLocalRedirectResult().WithLocalUrl(expectedLocalUrl);
2732
a.Should().Throw<Exception>().WithMessage(failureMessage);
2833
}
2934

3035
[Fact]
3136
public void WithPermanent_GivenExpectedPermanent_ShouldPass()
3237
{
3338
var result = new TestController().LocalRedirectPermanent(TestLocalUrl);
39+
3440
result.Should().BeLocalRedirectResult().WithPermanent(true);
3541
}
3642

@@ -39,18 +45,19 @@ public void WithPermanent_GivenUnexpectedPermanent_ShouldFail()
3945
{
4046
var actualPermanent = true;
4147
var expectedPermanent = false;
42-
4348
ActionResult result = new LocalRedirectResult(TestLocalUrl) { Permanent = actualPermanent };
49+
var failureMessage = FailureMessageHelper.ExpectedContextToBeXButY("LocalRedirectResult.Permanent", expectedPermanent, actualPermanent);
50+
51+
Action a = () => result.Should().BeLocalRedirectResult().WithPermanent(expectedPermanent, Reason, ReasonArgs);
4452

45-
var failureMessage = $"Expected LocalRedirectResult.Permanent to be {expectedPermanent} but was {actualPermanent}";
46-
Action a = () => result.Should().BeLocalRedirectResult().WithPermanent(expectedPermanent);
4753
a.Should().Throw<Exception>().WithMessage(failureMessage);
4854
}
4955

5056
[Fact]
5157
public void WithPreserveMethod_GivenExpectedPreserveMethod_ShouldPass()
5258
{
5359
var result = new TestController().LocalRedirectPreserveMethod(TestLocalUrl);
60+
5461
result.Should().BeLocalRedirectResult().WithPreserveMethod(true);
5562
}
5663

@@ -59,18 +66,19 @@ public void WithPreserveMethod_GivenUnexpectedPreserveMethod_ShouldFail()
5966
{
6067
var actualPreserveMethod = true;
6168
var expectedPreserveMethod = false;
62-
6369
ActionResult result = new LocalRedirectResult(TestLocalUrl) { PreserveMethod = actualPreserveMethod };
70+
var failureMessage = FailureMessageHelper.ExpectedContextToBeXButY("LocalRedirectResult.PreserveMethod", expectedPreserveMethod, actualPreserveMethod);
71+
72+
Action a = () => result.Should().BeLocalRedirectResult().WithPreserveMethod(expectedPreserveMethod, Reason, ReasonArgs);
6473

65-
var failureMessage = $"Expected LocalRedirectResult.PreserveMethod to be {expectedPreserveMethod} but was {actualPreserveMethod}";
66-
Action a = () => result.Should().BeLocalRedirectResult().WithPreserveMethod(expectedPreserveMethod);
6774
a.Should().Throw<Exception>().WithMessage(failureMessage);
6875
}
6976

7077
[Fact]
7178
public void WithPreserveMethod_GivenExpectedPermanentPreserveMethod_ShouldPass()
7279
{
7380
var result = new TestController().LocalRedirectPermanentPreserveMethod(TestLocalUrl);
81+
7482
result.Should().BeLocalRedirectResult().WithPermanent(true).WithPreserveMethod(true);
7583
}
7684
}

tests/FluentAssertions.AspNetCore.Mvc.Tests/OkObjectResultAssertions_Tests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public void Value_GivenUnexpectedValue_ShouldFail()
2121
var result = new TestController().Ok(TestValue);
2222

2323
Action a = () => result.Should().BeOkObjectResult().Value.Should().Be("xyx");
24+
2425
a.Should().Throw<Exception>();
2526
}
2627

@@ -38,6 +39,7 @@ public void ValueAs_GivenUnexpectedValue_ShouldFail()
3839
var result = new TestController().Ok(TestValue);
3940

4041
Action a = () => result.Should().BeOkObjectResult().ValueAs<string>().Should().Be("xyx");
42+
4143
a.Should().Throw<Exception>();
4244
}
4345

@@ -49,6 +51,7 @@ public void ValueAs_GivenWrongType_ShouldFail()
4951
"OkObjectResult.Value", typeof(int).FullName, typeof(string).FullName);
5052

5153
Action a = () => result.Should().BeOkObjectResult().ValueAs<int>().Should().Be(2);
54+
5255
a.Should().Throw<Exception>()
5356
.WithMessage(failureMessage);
5457
}

0 commit comments

Comments
 (0)