RSpec and Test Doubles
Test Double is a generic term where you replace a production object for testing purposes.
Using mocks in RSpec, even if you use verified doubles, is still prone to ending up with false positives. To prevent the problem of false positives in test suites, the advice is to avoid stubbing or mocking objects the team didn’t define.
Do not mock objects that you don’t own[¹]
The following are some of the problems that might suggest that a double is needed:
- Tests setup is complex during integration
- Tests are slowing down.
- Causes unwanted and/or problematic side effects in external systems (ie, a file system)
- Requires an Internet connection
[¹]: Steve Freeman, Nat Pryce (2009): Growing Object Oriented Design Guided by Tests (https://www.amazon.com/Growing-Object-Oriented-Software-Guided-Tests/dp/0321503627