question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fluent Assertion Should().Be() returns false positive

See original GitHub issue

Our 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:closed
  • Created 6 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
krajekcommented, Jan 9, 2018

@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 of null subject.

int? subject = null;
subject?.Should().Be(400);

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.

0reactions
NamrataKankariacommented, Jun 15, 2020

No. Why would you need a try/catch block at all?

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tips - Fluent Assertions
If your assertion ends with Should().BeTrue() , there is most likely a better way to write it. By having Should() as early as...
Read more >
FluentAssertions Should.Equal on collections, containing ...
Is there some workaround with Fluent Assertions? Or I have to use cycles in unit tests if I`m sure that nulls are normal...
Read more >
Fluent Assertions 5.0: The best unit test assertion library in the ...
You'll now find that all assertions start with Should() , e.g. ... In general, be prepared for discovering some false-positives that were ...
Read more >
Better assertions for your unit tests - Johan Smarius
Every assertion for Fluent Assertions should begin with the call to ... The precision of a number can result in false positive or...
Read more >
NET Unit Test Assertion Framework Comparison - Wright
The implementation for asserts are pretty straightforward. Unit tests will fail if an unexpected and uncaught Exception occurs. The failure ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found