C#. Fluent Assertions
Should
Fluent Assertions Should: Used to assert
that the actual result of an operation meets the expected
condition.
Basic Assertions
# Equal Assertions
result.Should().Be(expected);
# Null Assertions
object.Should().BeNull();
object.Should().NotBeNull();
# Not Equal
result.Should().NotBe(unexpected);
# Greater Than / Less Than
result.Should().BeGreaterThan(value);
result.Should().BeLessThan(value);Collection Assertions
# Contain Items
collection.Should().Contain(expectedItem);
# Be Empty / Have Count
collection.Should().BeEmpty();
collection.Should().HaveCount(expectedCount);
# Contain Exactly
collection.Should().ContainInOrder(expectedItems);Exception Assertions
Links:
There are 2 ways of testing exceptions:
- Using Action
- Using Invoking
Using Action
If you prefer the arrange-act-assert syntax, you can also use an action in your act part.
# Act
Action act = () => _sut.Foo2("Hello");
# Assert
act.Should().Throw<InvalidOperationException>()
.WithInnerException<ArgumentException>()
.WithMessage("whatever");Using Invoking
# Act & Assert
_sut.Invoking(y => y.Foo("Hello"))
.Should().Throw<InvalidOperationException>()
.WithMessage("Hello is not allowed at this moment");Object Assertions
# Be Equivalent To
actualObject.Should().BeEquivalentTo(expectedObject);
# Have Property
actualObject.Should().HaveProperty("PropertyName").Which.Should().Be(expectedValue);Installation
dotnet add package FluentAssertions