In a test, we are often testing (asserting) individual items such as an (int) age is correct or a string matches an expected value.
If we are practicing test-first development we’ll write our asserts first.
Approval tests allow us to go beyond simple asserts.
What if the thing we’re checking is not a simple value, for example that a pie chart image matches the input data? Or what if we want to use our human judgement to decide when something looks correct, something that is hard to codify in one or more basic asserts?
ApprovalTests for .NET can be install via NuGet. Once installed, it gives us a whole new world when it comes to checking the output of code.
For example, say we are developing a class to represent a stickman. We want to be able to tell an instance to raise left arm or raise right leg for example.
Example of Using Approval Tests
So lets start with a test:
public void ShouldHaveDefaultPosture()
var sut = new StickMan();
And an empty StickMan:
public class StickMan
Here we’re using xUnit.net (the [Fact] attribute) but you could be using NUnit for example.
The first thing to notice here is there is no traditional Assert method, instead we’re using Approval Tests to verify the state of the system under test (sut).
The other think to notice is the [UseReporter] attribute that tells Approval Tests to use a diff tool to display errors when a test fails.
If we run this test, we’ll get a diff tool opened:
Read full article...