Home > Mobile >  FluentAssertions .Pass declaration
FluentAssertions .Pass declaration

Time:12-24

After performing a test, we typically do an Assert.True(someCondition). When a condition check isn't necessary and just completion of the test is sufficient, I end a test with Assert.Pass().

I'm using FluentAssertions and have been doing true.Should().BeTrue();. That is starting to feel more like a hack and not in the correct spirit of the library.

Is there some other Fluent syntax I could use to accomplish .Pass() intention?

-Update: Based on feedback, there is nothing wrong with using .Pass() from XUnit or NUnit when using primarily FluentAssertions.

CodePudding user response:

There isn't any equivalent to Assert.Pass() in Fluent Assertions. true.Should().BeTrue() is a tautology and a weaker statement than Assert.Pass(). So between those two I would continue using Assert.Pass().

Another way to explicitly state in code that running to completion is success, is saying that no exceptions should occur.

// Arrange
var subject = ...

// Act
var act = () => subject.CalculateTaxes();

// Assert
act.Should().NotThrow<Exception>();

For more examples on how to assert on exceptions, see https://fluentassertions.com/exceptions/.

A third way is to simply abandon the requirement of an explicit assertion, this can e.g. be captured in the method name, e.g. calculating_taxes_does_not_throw.

  • Related