Ruby on Rails testing

Ruby on Rails / RSpec – the difference between stubs and mocks

Ruby on Rails / RSpec – the difference between stubs and mocks January 25, 2018Leave a comment

You may want to check how to use stubs and mocks with RSpec before diving in into this subject.

Syntax differences

The only one syntax difference is the first method. With stubs you would use allow while with mocks you would use expect

Usage differences

Stubs are used to avoid execution of other methods and make them return desired values. Mocks are used to check if given method was executed with desired arguments and frequency.


It’s always better to learn by the example so let’s use one. We will create class and sample test for it:

class Message
  def initialize(user: user)
    @user = user
  def send
    name = 'message')
  attr_reader :user

We don’t really want to call any of those three methods so we will stub them and then use mocks to check if they were executed properly.

require 'spec_helper'

describe Message do
  describe '#send' do
    it 'sends message' do
      user = instance_double(User)
      name = 'Tim'
      allow(user).to receive(:name).with(format: 'message').and_return(name)
      allow(user).to receive(:update_message_sent_at)
      alloW(Emailer).to receive(:send_message).with(name)
      message = user)

      expect(user).to have_received(:update_message_sent_at).once
      expect(user).to have_received(:name).with(format: 'message').once
      expect(Emailer).to have_received(:send_message).with(name).once

And that’s it! As you can see we used stubs to not execute original code and used mocks to check if given method was really invoked. Such test structure is named as spies – I will explain it tomorrow.

Having problems with tests for your Ruby on Rails application?

Hit me on twitter or use contact form and let me know how can I help you!

Download free RSpec & TDD ebook

Do you want to earn more or jump to the next level in your company? Do you know that testing skills are one of the most desired skills? There is only first step: start testing and do it right. My ebook can help you. Subscribe to the newsletter to get a free copy of the book.

Leave a Reply

Your email address will not be published. Required fields are marked *