If your assertion ends with Should().BeTrue(), there is most likely a better way to write it.
By having Should() as early as possible in the assertion, we are able to include more information in the failure messages.
Improved assertions
The examples below show how you might improve your existing assertions to both get more readable assertions and much more informative failure messages.
If you see something missing, please consider submitting a pull request.
Collections
Assertion
Improvement
actual.Any().Should().BeTrue();
actual.Should().NotBeEmpty();
Expected True, but found False.
Expected collection not to be empty.
Assertion
Improvement
actual.Any().Should().BeFalse();
actual.Should().BeEmpty();
Expected False, but found True.
Expected collection to be empty, but found {SomeProperty: 0, OtherProperty: }.
Collection {SomeProperty: 0, OtherProperty: Actual} should have an item matching (x.OtherProperty == value(UnitTests2.CollectionTests+<>c__DisplayClass5_0).expectedValue).
Expected Collection {SomeProperty: 0, OtherProperty: Unexpected} to not have any items matching (x.OtherProperty == value(UnitTests2.CollectionTests+<>c__DisplayClass7_0).unexpectedValue).
// now fails on empty collection;
actual.Should().OnlyContain(x => x.SomeProperty)
Expected True, but found False.
Expected collection to contain only items matching (x.OtherProperty == value(UnitTests2.CollectionTests+<>c__DisplayClass9_0).expectedValue), but {SomeProperty: 0, OtherProperty: Actual} do(es) not match.
Assertion
Improvement
actual.Contains(expected).Should().BeTrue();
actual.Should().Contain(expected);
Expected True, but found False.
Expected collection {"Actual"} to contain "Expected".
Assertion
Improvement
actual.Contains(unexpected).Should().BeFalse();
actual.Should().NotContain(unexpected);
Expected False, but found True.
Expected collection {"Expected"} to not contain "Expected".
Assertion
Improvement
actual.Any(x => x == unexpected).Should().BeFalse();
actual.Should().NotContain(unexpected);
Expected False, but found True.
Expected collection {"Unexpected"} to not contain "Unexpected".
Assertion
Improvement
actual.Count().Should().Be(k);
actual.Should().HaveCount(k);
Expected value to be 2, but found 3.
Expected collection to contain 2 item(s), but found 3.
Assertion
Improvement
actual.Count().Should().BeGreaterThan(k);
actual.Should().HaveCountGreaterThan(k);
Expected a value greater than 4, but found 3.
Expected collection to contain more than 4 item(s), but found 3.
Assertion
Improvement
actual.Count().Should().BeGreaterOrEqualTo(k);
actual.Should().HaveCountGreaterOrEqualTo(k);
Expected a value greater or equal to 4, but found 3.
Expected collection to contain at least 4 item(s), but found 3.
Assertion
Improvement
actual.Count().Should().BeLessThan(k);
actual.Should().HaveCountLessThan(k);
Expected a value less than 2, but found 3.
Expected collection to contain fewer than 2 item(s), but found 3.
Assertion
Improvement
actual.Count().Should().BeLessOrEqualTo(k);
actual.Should().HaveCountLessOrEqualTo(k);
Expected a value less or equal to 2, but found 3.
Expected collection to contain at most 2 item(s), but found 3.
Assertion
Improvement
actual.Count().Should().NotBe(k);
actual.Should().NotHaveCount(k);
Did not expect 3.
Expected collection to not contain 3 item(s), but found 3.
Assertion
Improvement
actual.Should().HaveCount(1);
actual.Should().ContainSingle();
Expected collection to contain 1 item(s), but found 3.
Expected collection to contain a single item, but found {"", "", ""}.
Assertion
Improvement
actual.Should().HaveCount(0);
actual.Should().BeEmpty();
Expected collection to contain 0 item(s), but found 3.
Expected collection to be empty, but found {"", "", ""}.
Assertion
Improvement
actual.Should().HaveCount(expected.Count());
actual.Should().HaveSameCount(expected);
Expected collection to contain 2 item(s), but found 3.
Expected collection to have 2 item(s), but found 3.
Collection {SomeProperty: 0, OtherProperty: Actual} should have an item matching (x.OtherProperty == value(UnitTests2.CollectionTests+<>c__DisplayClass39_0).expectedValue).
Expected collection to be empty, but found {SomeProperty: 0, OtherProperty: Expected}.
Expected Collection {SomeProperty: 0, OtherProperty: Expected} to not have any items matching (x.OtherProperty == value(UnitTests2.CollectionTests+<>c__DisplayClass41_0).expectedValue).
Expected collection to contain 1 item(s), but found 0.
Expected collection to contain a single item matching (x.OtherProperty == value(UnitTests2.CollectionTests+<>c__DisplayClass43_0).expectedValue), but no such item was found.
Expected collection to contain only items matching (x.OtherProperty != value(UnitTests2.CollectionTests+<>c__DisplayClass44_0).unexpectedValue), but {SomeProperty: 0, OtherProperty: Unexpected} do(es) not match.
Expected Collection {SomeProperty: 0, OtherProperty: Unexpected} to not have any items matching (x.OtherProperty == value(UnitTests2.CollectionTests+<>c__DisplayClass45_0).unexpectedValue).
Assertion
Improvement
actual.Should().NotBeNull().And.NotBeEmpty();
actual.Should().NotBeNullOrEmpty();
Expected collection not to be empty.
Expected collection not to be empty.
Assertion
Improvement
actual.ElementAt(k).Should().Be(expected);
actual.Should().HaveElementAt(k, expected);
Expected string to be "Expected" with a length of 8, but "Unexpected" has a length of 10.
Expected "Expected" at index 0, but found "Unexpected".
Assertion
Improvement
actual[k].Should().Be(expected);
actual.Should().HaveElementAt(k, expected);
Expected string to be "Expected" with a length of 8, but "Unexpected" has a length of 10.
Expected "Expected" at index 0, but found "Unexpected".
Assertion
Improvement
actual.Skip(k).First().Should().Be(expected);
actual.Should().HaveElementAt(k, expected);
Expected string to be "Expected" with a length of 8, but "Unexpected" has a length of 10.
Expected "Expected" at index 1, but found "Unexpected".
Expected collection to be equal to {SomeProperty: 2, OtherProperty: , SomeProperty: 1, OtherProperty: }, but {SomeProperty: 1, OtherProperty: , SomeProperty: 2, OtherProperty: } differs at index 0.
Expected collection {SomeProperty: 2, OtherProperty: , SomeProperty: 1, OtherProperty: } to be ordered" by SomeProperty" and result in {SomeProperty: 1, OtherProperty: , SomeProperty: 2, OtherProperty: }.
Expected collection to be equal to {SomeProperty: 1, OtherProperty: , SomeProperty: 2, OtherProperty: }, but {SomeProperty: 2, OtherProperty: , SomeProperty: 1, OtherProperty: } differs at index 0.
Expected collection {SomeProperty: 1, OtherProperty: , SomeProperty: 2, OtherProperty: } to be ordered" by SomeProperty" and result in {SomeProperty: 2, OtherProperty: , SomeProperty: 1, OtherProperty: }.
Expected collection to be equal to {"Expected", "Expected"}, but {"Actual", "Actual"} differs at index 0.
Expected collection to be equal to {SomeProperty: 1, OtherProperty: Expected, SomeProperty: 2, OtherProperty: Expected}, but {SomeProperty: 1, OtherProperty: Actual, SomeProperty: 2, OtherProperty: Actual} differs at index 0.
Assertion
Improvement
actual.Intersect(expected).Should().BeEmpty();
actual.Should().NotIntersectWith(expected);
Expected collection to be empty, but found {SomeProperty: 1, OtherProperty: Expected}.
Did not expect collection to intersect with {SomeProperty: 1, OtherProperty: Expected}, but found the following shared items {SomeProperty: 1, OtherProperty: Expected}.
Assertion
Improvement
actual.Intersect(expected).Should().NotBeEmpty();
actual.Should().IntersectWith(expected);
Expected collection not to be empty.
Expected collection to intersect with {SomeProperty: 1, OtherProperty: Expected}, but {SomeProperty: 1, OtherProperty: Actual} does not contain any shared items.
Expected dictionary {[0, ]} to contain value "expected".
Expected dictionary to contain value "expected" at key 0, but found "".
Exceptions
Assertion
Improvement
Action act = () => thrownew InvalidOperationException("Problems, errorCode2 and more Problems");
act.Should().ThrowExactly<InvalidOperationException>()
.Which.Message.Should().Contain("errorCode1");
Action act = () => thrownew InvalidOperationException("Problems, errorCode2 and more Problems");
// using wildcards
act.Should().ThrowExactly<InvalidOperationException>()
.WithMessage("*errorCode1*");
Expected string "Problems, errorCode2 and more Problems" to contain "errorCode1".
Expected exception message to match the equivalent of "*errorCode1*", but "Problems, errorCode2 and more Problems" does not.
Assertion
Improvement
Action act = () => thrownew InvalidOperationException("Problems, errorCode2 and more Problems");
act.Should().Throw<Exception>()
.Which.Message.Should().Contain("errorCode1");
Action act = () => thrownew InvalidOperationException("Problems, errorCode2 and more Problems");
// using wildcards
act.Should().Throw<Exception>()
.WithMessage("*errorCode1*");
Expected string "Problems, errorCode2 and more Problems" to contain "errorCode1".
Expected exception message to match the equivalent of "*errorCode1*", but "Problems, errorCode2 and more Problems" does not.
Assertion
Improvement
Action act = () => thrownew Exception("Problems, errorCode2 and more Problems", new InvalidOperationException());
act.Should().Throw<Exception>()
.Which.InnerException.Should().BeAssignableTo<ArgumentException>();
Action act = () => thrownew Exception("Problems, errorCode2 and more Problems", new InvalidOperationException());
act.Should().Throw<Exception>()
.WithInnerException<ArgumentException>();
Expected action to be assignable to System.ArgumentException, but System.InvalidOperationException is not.
Expected inner System.ArgumentException, but found System.InvalidOperationException with message "Operation is not valid due to the current state of the object."
Assertion
Improvement
Action act = () => thrownew Exception("Problems, errorCode2 and more Problems", new ArgumentNullException());
act.Should().Throw<Exception>()
.Which.InnerException.Should().BeOfType<ArgumentException>();
Action act = () => thrownew Exception("Problems, errorCode2 and more Problems", new ArgumentNullException());
act.Should().Throw<Exception>()
.WithInnerExceptionExactly<ArgumentException>();
Expected type to be System.ArgumentException, but found System.ArgumentNullException.
Expected inner System.ArgumentException, but found System.ArgumentNullException with message "Value cannot be null."
Expected string not to be <null> or whitespace, but found " ".
Assertion
Improvement
actual.Length.Should().Be(k);
actual.Should().HaveLength(k);
Expected value to be 4, but found 6.
Expected string with length 4, but found string "Actual" with length 6.
Types
Assertion
Improvement
actual.GetType().Should().Be(typeof(T));
actual.Should().BeOfType<T>();
Expected type to be UnitTests2.MyClass, but found UnitTests2.MyIdenticalClass.
Expected type to be UnitTests2.MyClass, but found UnitTests2.MyIdenticalClass.
Assertion
Improvement
actual.GetType().Should().NotBe(typeof(T));
actual.Should().NotBeOfType<T>();
Expected type not to be [UnitTests2.MyClass, UnitTests2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null], but it is.
Expected type not to be [UnitTests2.MyClass, UnitTests2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null], but it is.
Assertion
Improvement
(actual is T).Should().BeTrue();
actual.Should().BeAssignableTo<T>();
Expected True, but found False.
Expected object to be assignable to UnitTests2.MyClass, but UnitTests2.MyIdenticalClass is not.
Assertion
Improvement
(actual as T).Should().NotBeNull();
actual.Should().BeAssignableTo<T>();
Expected object not to be <null>.
Expected object to be assignable to UnitTests2.MyClass, but UnitTests2.MyIdenticalClass is not.
Assertion
Improvement
(actual is T).Should().BeFalse();
actual.Should().NotBeAssignableTo<T>();
Expected False, but found True.
Expected object to not be assignable to UnitTests2.MyClass, but UnitTests2.MyClass is.
Assertion
Improvement
(actual as T).Should().BeNull();
actual.Should().NotBeAssignableTo<T>();
Expected object to be <null>, but found SomeProperty: 1, OtherProperty: actual.
Expected object to not be assignable to UnitTests2.MyClass, but UnitTests2.MyClass is.
MSTest Migration
The examples below show how you might write equivalent MSTest assertions using Fluent Assertions including the failure message from each case. We think this is both a useful migration guide and a convincing argument for switching.
If you see something missing, please consider submitting a pull request.
Assert
MSTest
Fluent Assertions
Assert.IsTrue(actual);
actual.Should().BeTrue();
Assert.IsTrue failed.
Expected True, but found False.
MSTest
Fluent Assertions
Assert.IsFalse(actual);
actual.Should().BeFalse();
Assert.IsFalse failed.
Expected False, but found True.
MSTest
Fluent Assertions
Assert.IsNull(actual);
actual.Should().BeNull();
Assert.IsNull failed.
Expected object to be <null>, but found System.Object (HashCode=51904525).
MSTest
Fluent Assertions
Assert.IsNotNull(actual);
actual.Should().NotBeNull();
Assert.IsNotNull failed.
Expected object not to be <null>.
MSTest
Fluent Assertions
Assert.AreEqual(expected, actual);
actual.Should().Be(expected);
Assert.AreEqual failed. Expected:. Actual:.
Expected object to be SomeProperty: 2, OtherProperty: expected, but found SomeProperty: 1, OtherProperty: actual.
MSTest
Fluent Assertions
Assert.AreEqual(expected, actual, delta);
actual.Should().BeApproximately(expected, delta);
Assert.AreEqual failed. Expected a difference no greater than <0.5> between expected value <2> and actual value <1.25>.
Expected value 1.25 to approximate 2.0 +/- 0.5, but it differed by 0.75.
MSTest
Fluent Assertions
Assert.AreNotEqual(expected, actual);
actual.Should().NotBe(expected);
Assert.AreNotEqual failed. Expected any value except:. Actual:.
Did not expect object to be equal to SomeProperty: 1, OtherProperty: expected.
Expected collection to be a subset of {SomeProperty: 1, OtherProperty: item}, but items {SomeProperty: 2, OtherProperty: other} are not part of the superset.
Test method threw exception System.InvalidCastException, but exception System.ArgumentNullException was expected. Exception System.InvalidCastException: Specified cast is not valid.
Expected a to be thrown, but found a : System.InvalidCastException with message "Specified cast is not valid." at UnitTests2.ExceptionTests.<>c.b__1_0() in C:\Path\To\UnitTests\ExceptionTests.cs:line 31 at UnitTests2.ExceptionTests.<>c__DisplayClass1_0.b__1() in C:\Path\To\UnitTests\ExceptionTests.cs:line 34 at FluentAssertions.Specialized.ActionAssertions .InvokeSubjectWithInterception() .
Assert.ThrowsException failed. Threw exception InvalidCastException, but exception ArgumentNullException was expected. Exception Message: Specified cast is not valid. Stack Trace: at UnitTests2.ExceptionTests.<>c.b__2_0() in C:\Path\To\UnitTests\ExceptionTests.cs:line 44 at UnitTests2.ExceptionTests.<>c__DisplayClass2_0.b__1() in C:\Path\To\UnitTests\ExceptionTests.cs:line 47 at Microsoft.VisualStudio.TestTools.UnitTesting.Assert .ThrowsException[T](Action action, String message, Object[] parameters)
Expected a to be thrown, but found a : System.InvalidCastException with message "Specified cast is not valid." at UnitTests2.ExceptionTests.<>c.b__1_0() in C:\Path\To\UnitTests\ExceptionTests.cs:line 31 at UnitTests2.ExceptionTests.<>c__DisplayClass1_0.b__1() in C:\Path\To\UnitTests\ExceptionTests.cs:line 34 at FluentAssertions.Specialized.ActionAssertions .InvokeSubjectWithInterception() .
Test method threw exception System.InvalidCastException, but exception System.ArgumentException or a type derived from it was expected. Exception System.InvalidCastException: Specified cast is not valid.
Expected a to be thrown, but found a : System.InvalidCastException with message "Specified cast is not valid." at UnitTests2.ExceptionTests.<>c.b__3_0() in C:\Path\To\UnitTests\ExceptionTests.cs:line 57 at UnitTests2.ExceptionTests.<>c__DisplayClass3_0.b__1() in C:\Path\To\UnitTests\ExceptionTests.cs:line 60 at FluentAssertions.Specialized .ActionAssertions.InvokeSubjectWithInterception().
Expected string to be "expectedMessage" with a length of 15, but "actualMessage" has a length of 13.
Expected exception message to match the equivalent of "expectedMessage", but "actualMessage" does not.
xUnit Migration
The examples below show how you might write equivalent xUnit assertions using Fluent Assertions including the failure message from each case. We think this is both a useful migration guide and a convincing argument for switching.
If you see something missing, please consider submitting a pull request.
Assert.NotEqual() Failure: Values are equal Expected: Not 1 Actual: 1
Did not expect actual to be 1.
xUnit
Fluent Assertions
Assert.Null(actual);
actual.Should().BeNull();
Assert.Null() Failure: Value is not null Expected: null Actual: Object { }
Expected actual to be <null>, but found System.Object (HashCode=33420276).
xUnit
Fluent Assertions
Assert.NotNull(actual);
actual.Should().NotBeNull();
Assert.NotNull() Failure: Value is null
Expected actual not to be <null>.
xUnit
Fluent Assertions
Assert.Same(expected, actual);
actual.Should().BeSameAs(expected);
Assert.Same() Failure: Values are not the same instance Expected: { OtherProperty = "expected", SomeProperty = 2 } Actual: { OtherProperty = "actual", SomeProperty = 1 }
Expected actual to refer to { OtherProperty = "expected", SomeProperty = 2 }, but found { OtherProperty = "actual", SomeProperty = 1 }.
xUnit
Fluent Assertions
Assert.NotSame(expected, actual);
actual.Should().NotBeSameAs(expected);
Assert.NotSame() Failure: Values are the same instance
Did not expect actual to refer to { OtherProperty = "expected", SomeProperty = 1 }.
xUnit
Fluent Assertions
Assert.Contains(expected, actual);
actual.Should().Contain(expected);
Assert.Contains() Failure: Item not found in collection Collection: [1, 2] Not found: 3
Expected actual {1, 2} to contain 3.
xUnit
Fluent Assertions
Assert.DoesNotContain(unexpected, actual);
actual.Should().NotContain(unexpected);
Assert.DoesNotContain() Failure: Item found in collection ↓ (pos 1) Collection: [1, 2] Found: 2
Expected actual {1, 2} to not contain 2.
xUnit
Fluent Assertions
Assert.Empty(actual);
actual.Should().BeEmpty();
Assert.Empty() Failure: Collection was not empty Collection: [1]
Expected actual to be empty, but found at least one item {1}.
xUnit
Fluent Assertions
Assert.NotEmpty(actual);
actual.Should().NotBeEmpty();
Assert.NotEmpty() Failure: Collection was empty
Expected actual not to be empty.
xUnit
Fluent Assertions
Assert.Single(actual);
actual.Should().ContainSingle();
Assert.Single() Failure: The collection contained 2 items Collection: [1, 2]
Expected actual to contain a single item, but found {1, 2}.
xUnit
Fluent Assertions
Assert.IsType<T>(actual);
actual.Should().BeOfType<T>();
Assert.IsType() Failure: Value is not the exact type Expected: typeof(string) Actual: typeof(int)
Expected type to be System.String, but found System.Int32.
xUnit
Fluent Assertions
Assert.IsAssignableFrom<T>(actual);
actual.Should().BeAssignableTo<T>();
Assert.IsAssignableFrom() Failure: Value is an incompatible type Expected: typeof(string) Actual: typeof(int)
Expected actual to be assignable to System.String, but System.Int32 is not.
xUnit
Fluent Assertions
Assert.Throws<TException>(() => ...);
action.Should().ThrowExactly<TException>();
Assert.Throws() Failure: Exception type was not an exact match Expected: typeof(System.InvalidOperationException) Actual: typeof(System.ArgumentException)
Expected type to be System.InvalidOperationException, but found System.ArgumentException.