Create a gist now

Instantly share code, notes, and snippets.

TL; DR: "mock" and "stub" were the common terminology when RSpec's API was first created but we've realized that using them to create fake objects causes confusion and that double is a much better term.

I wasn't around when RSpec's original APIs were created, so this is a bit of guess work, but here's my understanding.

When RSpec's mocking API was first created, the two common terms used for what we now commonly call "test doubles" were mocks and stubs. Mocking was a relatively new technique at the time, and the terminology was still evolving. RSpec provided both mock and stub to create a fake object to use as a collaborator in your test.

However, mocks aren't stubs. They are different things. Furthermore, the mock vs stub distinction isn't a property of the object, it's a property of an individual method. That is, a test double can have some methods that have been mocked (which RSpec will verify have been called at the end of the example) and some methods that have been stubbed (which return canned responses). Thus, I think it creates confusion to have mock return a test double (which may in fact only have stubbed methods and not be a mock at all).

Meanwhile, Gerard Meszaro later introduced the term "test double" to provide a single, unified term for fake objects used as stand-ins in place of real objects for testing. Martin Fowler has a nice article on it. RSpec added double at some point after Meszaro's book introduced the term. Since then, mock and stub have been aliases for double but there hasn't been a strong reason to deprecate or remove them.

In RSpec 3 we're introducing some new types of doubles that auto-verify the methods you mock or stub against a defined interface, and we're calling these instance_double and class_double. We don't also want to have instance_mock, class_mock, instance_stub and class_stub, and thought it would be odd to retain the mock and stub alias for double without maintaning feature parity. So we decided to deprecate them in preparation for RSpec 3.

Of course, while RSpec 3 no longer provides mock and stub aliases of double, it's easy to define these aliases on your own if you'd like to keep using them:

module DoubleAliases
  def mock(*args, &block)
    double(*args, &block)
  end
  alias stub mock
end

RSpec.configure do |config|
  config.include DoubleAliases
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment