Skip to content

Commit bd5f61d

Browse files
committed
Fix Several assertion not including reason text.
1 parent 5cc6e1b commit bd5f61d

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

src/FluentAssertions.AspNetCore.Mvc/FailureMessages.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/FluentAssertions.AspNetCore.Mvc/FailureMessages.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
<value>Expected {context} to be of type {0}, but no value was supplied.</value>
134134
</data>
135135
<data name="CommonTypeFailMessage" xml:space="preserve">
136-
<value>Expected {context} to be of type {0} but was {1}.</value>
136+
<value>Expected {context} to be of type {0} but was {1}{reason}.</value>
137137
</data>
138138
<data name="ConvertibelModelFailMessage" xml:space="preserve">
139139
<value>Expected {context} to be ActionResult&lt;TValue&gt; with type {0}{reason} but was {1}.</value>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public void BeActionResult_GivenUnexpectedType_ShouldFail()
6262
{
6363
var mock = new Mock<IConvertToActionResult>();
6464
var result = mock.Object;
65-
var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundY(
66-
"result", "Microsoft.AspNetCore.Mvc.ActionResult`1[System.Object]", result.GetType().FullName);
65+
var failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason(
66+
"result", typeof(ActionResult<object>), result.GetType());
6767

6868
Action action = () => result.Should().BeActionResult<object>(Reason, ReasonArgs);
6969

tests/FluentAssertions.AspNetCore.Mvc.Tests/Helpers/FailureMessageHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ internal static string ExpectedToContainItem(string context, object list, string
8888

8989
internal static string ExpectedContextTypeXButFoundY(string context, Type expected, Type actual)
9090
{
91-
return ExpectedContextTypeXButFoundY(context, expected.FullName, actual.FullName);
91+
return $"Expected {context} to be of type {Formatter.ToString(expected)} but was {Formatter.ToString(actual)}.";
9292
}
9393

94-
internal static string ExpectedContextTypeXButFoundY(string context, string expected, string actual)
94+
internal static string ExpectedContextTypeXButFoundYWithReason(string context, Type expected, Type actual)
9595
{
96-
return $"Expected {context} to be of type {expected} but was {actual}.";
96+
return $"Expected {context} to be of type {Formatter.ToString(expected)} but was {Formatter.ToString(actual)} because it is 10.";
9797
}
9898

9999
internal static string ExpectedContextTypeXButFoundNull(string context, Type expectedType)

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,38 @@ public void WithContentType_GivenUnexpected_ShouldFail()
155155
a.Should().Throw<Exception>()
156156
.WithMessage(failureMessage);
157157
}
158+
159+
160+
[Fact]
161+
public void WithDeclaredType_GivenExpected_ShouldPass()
162+
{
163+
var expectedType = typeof(string);
164+
var result = new ObjectResult(TestValue)
165+
{
166+
DeclaredType = expectedType
167+
};
168+
169+
result.Should().BeObjectResult().WithDeclaredType(expectedType);
170+
}
171+
172+
[Fact]
173+
public void WithDeclaredType_GivenUnexpected_ShouldFail()
174+
{
175+
var expectedType = typeof(int);
176+
var declaredType = typeof(string);
177+
var result = new ObjectResult(TestValue)
178+
{
179+
DeclaredType = declaredType
180+
};
181+
string failureMessage = FailureMessageHelper.ExpectedContextTypeXButFoundYWithReason(
182+
"ObjectResult.DeclaredType",
183+
expectedType,
184+
declaredType);
185+
186+
Action a = () => result.Should().BeObjectResult().WithDeclaredType(expectedType, Reason, ReasonArgs);
187+
188+
a.Should().Throw<Exception>()
189+
.WithMessage(failureMessage);
190+
}
158191
}
159192
}

0 commit comments

Comments
 (0)