Fairly recently we investigated a runtime error while running cron jobs against our subscription purchases. The error stemmed from a class mismatch when a method
method_foo was returning a
SubscriptionFoo object when we were expecting a
SubscriptionBar object. The solution was fairly straight forward, however writing a well-scoped test for
method_foo and its return value was not. Why? Because it is often overlooked to write well-scoped tests for relatively large applications in favor of writing narrow-scoped tests that strictly test what you want. Tests should be just as scalable, flexible and maintainable as our main code.
The initial attempt was to test
method_foo's class return types:
The code was relatively simple:
describe FooBar do describe '#method_foo' do context 'when it should return a SubscriptionTypeFoo' do ...