Working with assertion frameworks
While the testify/mock
functionality is useful for creating mocks, testify
is best known for its assertion framework. In this section, we will explore some common assertion frameworks and how we can use them to further streamline and expand our tests.
So far, we have been writing our verifications using if
statements and invoking the correct failure method on the testing.T
parameter:
// Assert if err != nil { t.Fatal(err) }
This approach is simple, but it does have the following disadvantages:
- Repetition: A lengthy or complex test will end up making multiple assertions. We will then have to repeat this error assertion block multiple times, making the test verbose.
- Difficult to make advanced assertions: We want to have the same fine-grained control over the verifications we undertake on our mocks throughout the rest of the test.
- Completely different approach to other languages: This approach is completely different...