Assert
An assert step must follow at least one act step.
Both sync and async is supported as well as additional And steps.
And steps are executed in parallel and will return an aggregate exception if more than one assertion fails.
[Fact(DisplayName = "SerializeAsync can work with anonymous objects")]
public Task ExampleTest5() =>
new { Name = "Widget1", Cost = 12.50 }
.Arrange()
.Act(SerializeAsync)
// standard action where any assertions can be made
.Assert(Assert.NotEmpty)
// many and steps are supported
.And(json =>
Assert.Contains(
expectedSubstring: "Widget1",
actualString: json,
StringComparison.Ordinal
)
)
.And(json =>
Assert.Contains(
expectedSubstring: "12.5",
actualString: json,
StringComparison.Ordinal
)
);
The standard assert step is a simple Action and succeeds as long as nothing throws.
Expression Based Asserts
Another style of assertion is using expressions.
[Fact(DisplayName = "SerializeAsync can work with anonymous objects")]
public Task ExampleTest6() =>
new { Name = "Widget1", Cost = 12.50 }
.Arrange()
.Act(SerializeAsync)
// any expression can be used to form the assertion
.Assert(json => !string.IsNullOrWhiteSpace(json))
.And(json => json.Contains("Widget1"))
.And(json => json.Contains("12.5"));
Asserting against exceptions
On the unhappy path throw can be used to assert against exceptions,
[Fact(DisplayName = "Divide by 0 fails")]
public Task ExampleTest7() =>
(dividend: 3, divisor: 0)
.Arrange()
.Act(parts => parts.dividend / parts.divisor)
// errors can be asserted against as well
.Throw<DivideByZeroException>()
.Assert(e => e.Message == "Attempted to divide by zero.");
If the act step does not fail the test will fail.