In my previous post, we saw how value and type based assertions works in xUnit. In this post I’m going to focus on assertions that check whether or not something happened. Specifically we look at assertions to check if an event is raised or an exception is thrown.
Asserting if An Event Is Raised
Let’s consider this class as an example.
We can write a unit test to test that a specific event have risen like this.
The first assertion is Assert.Raises
, it verifies that a event with the exact event args is raised. It requires a delegate for subscription, another delegate to unsubscribe. Finally it accepts another delegate that execute the action. The Assert.RaisesAny
verifies that an event with the exact or a derived event args is raised. The accepted parameter for this method is the same as previous ones. Finally Assert.RaisesAsync
does the same thing as Assert.Raises
but in an asynchronous manner. We can see that instead of Action testCode
it receive a Func<Task> testCode
.
Assertions Related to Exception
Let’s assume we have this class that does nothing but throwing exception.
Let see how we can test to see if the expected exceptions are raised when we use this class.
The first method uses Assert.Throws
, it verifies that the exact exception is thrown (and not a derived exception type). Assert.ThrowsAny
on the other hand verifies that the exact exception or a derived exception type is thrown. There are also the asynchronous version of these methods, namely Assert.ThrowsAsync
and Assert.ThrowsAnyAsync
. We can also use Record.Exception
by passing the action in to see if it throws specific exception.
Checking For Changes In A Property Or A Collection
There are some assertion methods that can be used to check to see if a property changed on an object. The same thing exist to check if a collection has change in some way.
The first method uses Assert.All
, it verifies that all items in the collection pass when executed against action. Assert.Collection
verifies that a collection contains exactly a given number of elements, which meet the criteria provided by the element inspectors. In other word we pass a series of actions into the assert to check to see if elements of the collection are as expected. Lastly there is the Assert.PropertyChanged
, Verifies that the provided object raised INotifyPropertyChanged.PropertyChanged
as a result of executing the given test code.
Third Party Tools For Assertions
There are some library that make our assertions more readable. By using those libraries our assertions are going to read almost like a normal sentence. The first library that I’m a fan of is FluentAssertions. There’s also Shouldly which worth taking a look at.
We can see how much more readable this way of assertion is.
Summary
In this post we saw how we can assert that an event is raised using xUnit. We also saw how to check for situations when an exception is thrown. Then I moved on to a few methods that helps up check if a property of a collection has changed in a specific way.