Skip to content

Commit 868a26d

Browse files
committedNov 1, 2020
Add BeObjectResult to ActionResultAssertions<TValue>.
1 parent c0997a9 commit 868a26d

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed
 

‎samples/FluentAssertions.AspNetCore.Mvc.Sample.Tests/ProductController_Tests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public void GetActionResultOfT_OnFalse_Returns_Data()
2525

2626
var result = controller.GetActionResultOfT(model, returnError);
2727

28-
result.Should().BeConvertibleTo<ObjectResult>()
29-
.And.Value.Should().BeSameAs(model);
28+
result.Should().BeObjectResult()
29+
.Value.Should().Be(model);
3030
}
3131

3232
[Fact]

‎src/FluentAssertions.AspNetCore.Mvc/ActionResultAssertionsOfTValue.cs

+20-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ public AndWhichConstraint<ActionResultAssertions<TValue>, TActionResult> BeConve
7979

8080
return new AndWhichConstraint<ActionResultAssertions<TValue>, TActionResult>(this, (TActionResult)convertResult);
8181
}
82+
83+
/// <summary>
84+
/// Asserts that the <see cref="ActionResult{TValue}.Result"/> is type of <see cref="ObjectResult"/>.
85+
/// </summary>
86+
/// <param name="reason">
87+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
88+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
89+
/// </param>
90+
/// <param name="reasonArgs">
91+
/// Zero or more objects to format using the placeholders in <paramref name="reason"/>.
92+
/// </param>
93+
[CustomAssertion]
94+
public ObjectResultAssertions BeObjectResult(string reason = "", params object[] reasonArgs)
95+
{
96+
var result = BeConvertibleTo<ObjectResult>(reason, reasonArgs).Which;
97+
98+
return new ObjectResultAssertions(result);
99+
}
100+
101+
#endregion Public Methods
82102
}
83-
#endregion Public Methods
84103
}

‎tests/FluentAssertions.AspNetCore.Mvc.Tests/ActionResultAssertionsOfTValue_Tests.cs

+22
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@ public void BeConvertibleTo_ShouldBeTheConvertedObject()
7272

7373
actual.Should().BeSameAs(expectation);
7474
}
75+
76+
[Fact]
77+
public void BeObjectResult_GivenActionResultWithObjectResult_ShouldPass()
78+
{
79+
var result = new ActionResult<object>(new object());
80+
81+
result.Should().BeObjectResult(Reason, ReasonArgs);
82+
}
83+
84+
[Fact]
85+
public void BeObjectResult_GivenActionResultWithNotObjectResult_ShouldFail()
86+
{
87+
var result = new ActionResult<object>(new BadRequestObjectResult(new object()));
88+
var failureMessage = FailureMessageHelper.ExpectedContextToBeConvertible(
89+
"result", typeof(ObjectResult).FullName, typeof(BadRequestObjectResult).FullName);
90+
91+
Action action = () => result.Should().BeObjectResult(Reason, ReasonArgs);
92+
93+
action.Should().Throw<Exception>()
94+
.WithMessage(failureMessage);
95+
}
96+
7597
#endregion Public Methods
7698
}
7799
}

0 commit comments

Comments
 (0)
Failed to load comments.