Test-Doubles and Stubbing in Ruby On Rails Tests
Test Double is a generic term for any case where you replace a production object for testing purposes. — Martin Fowler
Stubbing out methods on the object being tested is a symptom of bad internal APIs or poor coupling.
Testing private methods is often a symptom of “God Objects.” God objects are complex and large objects with many different behaviors.
The private method can generally be isolated to its testable class or value object:
Using stub chains is a smell you should be using dependency injection (and injecting test doubles):
Use verified doubles to keep the object’s API changes in sync. Use dependency injection to inject the Test-Doubles. With ActiveRecord models, this is not possible (their accessors are defined by checking the database schema). Use a tool like factoryBot to keep in sync with the database schema changes and centralize such object creation in tests.