# How to Write Future-proof Mocks in RSpec 3

DevFeed: [How to Write Future-proof Mocks in RSpec 3](<https://devfeed.tech/articles/how-to-write-future-proof-mocks-in-rspec-3-21051.md>)

Original publisher: [Read original article](<https://jakeyesbeck.com/2015/11/22/how-to-write-futureproof-mocks/>)

Published: 2015-11-22T12:00:00Z

Content type: tutorial

Language: en

Sources: [Jake Yesbeck](<https://devfeed.tech/sources/jake-yesbeck.md>)

Topics: [RSpec](<https://devfeed.tech/topics/rspec.md>), [Development](<https://devfeed.tech/topics/development.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [development](<https://devfeed.tech/tags/development.md>), [tests](<https://devfeed.tech/tags/tests.md>)

## AI overview

This tutorial explains how to write future-proof mocks in RSpec 3. It demonstrates how ordinary doubles can allow tests to pass even after an external service changes its method signature, and recommends verifying doubles to ensure mocked methods exist on the underlying object.

## Source excerpt

Tests are an important component in most software applications. Whether tests drive the development, or are strapped on after the fact, tests need to be reliable for future development to progress. An application's complexity rises with time. During and after this increase, a rock solid test suite is crucial. Let the Dogs Out Given an application that deals with Dogs: class Dog end Keeping track of grooming statistics about this application's dogs is the obvious cash cow. But that sounds very tedious and complicated, so we can assume that a GroomingService exists that exposes a single Groomer object: module GroomingService class Groomer def initialize(dog) @dog = dog end def groom # Some complex grooming logic end end end This service was included by the groomer gem and will become a core piece of the application. To keep everyone in the project sane and well rested, a robust spec suite is needed. Spec It Up Using RSpec and the GroomingService, a very basic test can be written to make sure the code works as expected. Introducing the GroomingService into the Dog class could look like: class Dog def groom GroomingService::Groomer.new(self).groom end end The first test should be to make sure that when the groom method on a Dog is called, it initializes and calls groom out to the GroomService::Groomer object. When asserting that a certain set of methods is called on a particular object, the built in RSpec double method is very handy. The double method returns a stand-in object to assert allow and expect messages against. Any method that is called on the test double which is not explicitly allowed or expected results in an error. describe Dog do subject { described_class.new } describe '#groom' do let(:groomer) { double(GroomingService::Groomer) } it 'initializes and calls groom' do expect(GroomingService::Groomer).to receive(:new) { groomer } expect(groomer).to receive(:groom) subject.groom end end end As expected, this test passes beautifully: $: rspec dog_spec.rb Dog