How to replace Assert.Fail() with FluentAssertions

Restructure the test to utilize the .ShouldThrow<> assertion extension.

[TestMethod, TestCategory("ImportantTest")]
public void MethodToTest_Circumstances_ExpectedResult() {
    // Arrange
    var variable1 = new Type1() { Value = "hello" };
    var variable2 = new Type2() { Name = "Bob" };

    // Act
    Action act = () => MethodToTest(variable1, variable2);       

    // Assert
    // This method should have thrown an exception
    act.ShouldThrow<DataException>()
       .WithMessage(Constants.DataMessageForMethod);
    // test that variable1 was changed by the method
    variable1.Should().NotBeNull();
    variable1.Value.Should().Be("Hello!");
    // test that variable2 is unchanged because the method threw an exception before changing it
    variable2.Should().NotBeNull();
    variable2.Name.Should().Be("Bob");
}

In the above example, if the expected exception is not thrown the the assertion would fail, stopping the test case.

You should review the documentation on asserting exceptions to get a better understanding of how to use the library.


Following the example in here, he just dealt away with the Assert.Fail -- and use action and .ShouldThrow http://www.continuousimprover.com/2011/07/why-i-created-fluent-assertions-in.html