[rspec-users] Factories vs. stubs/mocks

J. B. Rainsberger me at jbrains.ca
Fri Sep 3 19:38:48 EDT 2010

On Mon, Aug 30, 2010 at 14:09, Rob Biedenharn
> And THAT is the problem with using mocks (or stubs) for this.  You run the
> very real risk of specifying the implementation details in the structure of
> the spec/test.  There is a problem when you've limited the refactoring that
> can be done without altering the spec/test. Since Widget.find(:all) and
> Widget.all should always have the same result, the mere presence of the mock
> has cut off one possible refactoring.  (The one that I distaste most limits
> changing .find(params[:id]) to .find_by_id(params[:id]) by mocking/stubbing
> the call to find.)

On the contrary: by stubbing you've uncovered that you had used a
low-level API (#find) instead of a high-level API (#all). This
prompted you to make a conscious choice, which you did, and I prefer
the choice you made, to invoke #all instead of #find.

…and THAT is the magic of using stubs and expectations for this.
J. B. (Joe) Rainsberger :: http://www.jbrains.ca ::
Diaspar Software Services :: http://www.diasparsoftware.com
Author, JUnit Recipes
2005 Gordon Pask Award for contribution to Agile practice :: Agile
2010: Learn. Practice. Explore.

More information about the rspec-users mailing list