RSpec Stubs with no_args

We’ve been getting pretty particular about our stub/mock expectations at work. A few months ago I would been perfectly happy with:

TwitterGateway.stub(:new).and_return(double)

I didn’t worry about specifying that I didn’t pass any arguments to the constructor. After it was pointed out that the stub didn’t really fully specify its’ expectations I changed to this style:

TwitterGateway.stub(:new).with().and_return(double)

Then a colleague pointed out a nice bit of syntactic sugar. You can simply specify a no_args matcher if no arguments are passed in:

TwitterGateway.stub(:new).with(no_args).and_return(double)

A more complete stub with better readability. Reminds me of how I fell in love with RSpec the first time I saw it back in 2006.