Partial Stubs

Learn about partial stubs, stubs classes, and multiple partial stubs.

We might use a full double object to stand in for an entire object unavailable or prohibitively expensive to create or call in the test environment. We can also take advantage of how Ruby allows us to open up existing classes and objects to add or override methods. It’s easy to take a “real” object and stub out only the methods we need. This is extraordinarily useful when it comes to actual uses of stub objects.

In RSpec, partial stubs are managed with the allow method:

Bud1�lsdsclboolmodelsdsclbool @� @� @� @E�DSDB `� @� @� @
Adding first stub to stubs describe block in project_spec file

This test passes. Line 12 sets up the stub, and the stub intercepts the project.name call in line 13 to return nil and never even gets to the project name.

Note: This is not a smart test. We’re just verifying that the stubbing works.

Partial doubles verification

If mocks.verify_partial_doubles = true is set in the spec_helper.rb configuration file, which should be the default, then ...