Fluent Assertion Should().Be() returns false positive
See original GitHub issueOur Team uses Fluent Assertions alongside XUnit for unit testing. In the given code below, we are trying to test the Status Code 400 (Bad Request) from our controllers by passing null to one of our argument to the Controller method.
Unit Test:
[Fact]
public async Task PartiallyUpdatePerson_InvalidJsonPatchDocument_ReturnsStatusCode400()
{
var id = new Guid("47217150-1eff-4c7e-84a3-2b57001a18d6");
var result = await sut.PartiallyUpdatePerson(id, null) as ObjectResult;
result?.StatusCode.Should().Be(400);
}
SUT:
[HttpPatch("{id}", Name = "PartiallyUpdatePerson")]
public async Task<IActionResult> PartiallyUpdatePerson(Guid id, [FromBody] JsonPatchDocument<PersonPatchDto> personToPatch)
{
if (personToPatch == null || id == Guid.Empty)
{
return BadRequest();
}
.
.
.
.
return NoContent();
}
This unit test passes and everyone is happy with it.
The problem is when we try to change the Should().Be() assertion value. If we change from 400 to anything other than that, we expect the Test to fail. But it still passes with Statuscode <> 400.
To Summarize this is what is happening now:
result?.StatusCode.Should().Be(400)
--> Test Run Passes
result?.StatusCode.Should().Be(401)
--> Test Run Still Passes where it should have failed
On the other hand, if we use the regular assertion,
Assert.True(result?.StatusCode == 400)
--> Test Run Passes
Assert.True(result?.StatusCode == 401)
--> Test Run Fails
I’m not sure if we’re doing anything wrong or if there is an issue with Should().Be()
. Please let me know what you think about it.
Thanks, Balaji
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (8 by maintainers)
Top GitHub Comments
@dennisdoomen you added
requires-clarification
tag, but the issue is clear to me. @SaroTasciyan explained, that the problem was with the usage of operator .? which does not call right-hand side of expression at all, in case ofnull
subject.Above code does not throw. FA itself cannot help here. I would suggest closing the issue here. I will check if static analyzer for FA catches this one, and report an issue in their repository if it does not.
For statements other than assert statements. Like trying to access an element and do some operation etc and later using fluent assertion statement to verify our test