Difference between Mocks and Stubs in RSpec

What is the difference, how and when to apply?

Stub

allow(object).to receive(:method_name)
allow(object).to receive(:method_name).and_return(example_value)

Mock

expect(object).to receive(:method_name)
expect(object).to receive(:method_name).and_return(example_value)

Double

describe UpdateObject do
let(:external_api) { double }
before do
allow(ExternalApi).to recieve(:new).and_return(external_api)
allow(external_api).to recieve(:some_api_call)
end
it 'returns success' do
expect(subject.call).to eq(:success)
end
end

--

--

Software Engineer. Interested in Microservices, Distributed Systems and Cloud Services.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kirill Shevchenko

Software Engineer. Interested in Microservices, Distributed Systems and Cloud Services.