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, there are two kinds of stubs. 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, for example, 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 that responds to only certain methods, ...

Get Rails 5 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.