Creating Stubs

A stub is a replacement for all or part of an object that prevents a normal method call from happening and instead returns a value that is preset when the stub is created. In RSpec, as in many Ruby double libraries, there are two kinds of fake objects. You can create entire objects that exist only to be stubs, which we’ll call full doubles, or you can stub specific methods of existing objects, which we’ll call partial doubles.

A partial double is useful when you want to use a “real” ActiveRecord object but you have one or two dangerous or expensive methods you want to bypass. A full double is useful when you’re testing that your code works with a specific API rather than a specific object—by passing in a generic object ...

Get Rails 4 Test Prescriptions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.