Skip to content
This repository

rspec-mocks provides a framework for stubbing methods and setting message expectations (mocking).

A bit of terminology

A Test Double is an object that stands in for another object in a test.

A Mock Object is generally understood to be a Test Double on which you can set self-verifying expectations.

A Stub is generally understood to be a Test Double on which you can define known values to return in response to a message (or method call, if you prefer).

A Test Specific Subclass is generally understood to be a subclass of the real class that you are testing, instrumented with stub and mock object behaviour. In Ruby we can add that instrumentation via mixins (sometimes called partial mocking or partial stubbing rather than subclasses, so we get the feel of working with a real object, even though it doesn’t really have the same ancestor stack as the real object.

This can all be very confusing because in conversation we tend to talk about stubbing a method or mocking a method, and we might stub one method and mock another on the same object. So is that object a mock or a stub?

The answer is … it doesn’t matter! Whether it’s a Test Double or a real object, we get the same result by mixing in modules that provide method-level stubbing and mocking services. The important thing to remember is that when we set a message expectation (i.e. mock a method), the example will fail if that message is not received by the end of the example, but when we stub a method, you’ll get no feedback as to whether that method is called or not.

Something went wrong with that request. Please try again.