Heavy mocking and stubbing comes with a cost: you can’t say anything about collaboration features of well-unit-tested objects without integration and end-to-end tests. This talk covers stubbing, mocking and spying that verifies whether the faked methods exist on the actual object, take the right number of arguments and whether tests for a given class verify the behaviour that the class’s doubles pretend to have; with these facilities writing a system from outside in is demonstrably enjoyable.
Whether you like to write well-isolated (and fast!) unit tests or need to implement the outside of a system without having the inside nits-and-bolts in place beforehand there’s a plethora of stubbing and mocking libraries to choose from. Unfortunately, heavy mocking and stubbing comes with a cost: even with the most well-tested-in-isolation objects you can’t really say anything about their proper wirings without some integration and end-to-end tests – and writing the former is often not a very enjoyable experience.
This talk covers stubbing, mocking and spying that goes the extra mile and verifies whether your fakes have any connection with reality – whether the faked methods exist on the actual object, whether they take the right number of arguments and even whether tests for a given class verify the behaviour that the class’s fakes pretend to have. Both RSpec verified doubles and Bogus’s contract tests are covered; a change to a method’s name or its signature will make all of the related fakes complain, all the missing pieces of a system written from outside in will present themselves ready to be implemented, and a subset of mocks and stubs of your choice will verify that their production counterparts work as assumed.
This talk presents integration tests and different types of test doubles (and which use when if you follow CQRS). It also shows how to use RSpec’s verified doubles (with their pros and cons) and Bogus’s fakes, including faking roles and verifying contract tests based on the fakes’ configuration.