RSpec Mocks, Stubs, Let & Doubles

Steven Klavins
3 min readApr 6, 2020

A few useful things to consider when testing code are mocks, stubs, and doubles. I felt it would be useful to cover these as they have alluded me for a while now, these concepts are used across multiple testing tools however for this blog post I will be using Ruby’s RSpec.

Let’s

Probably the most simple concept I will discuss here is ‘let’, this, for example, allows us to create a new instance of a class which is reusable within our RSpec tests. These are particularly useful as they can cut down our test code dramatically, here is an example;

let(:our_new_instance_of_a_class) { ClassName.new (1, 2, “param_3”) }

This example clearly illustrates why this is so useful, why? Could you imagine having to write this code over and over in every single test, seems like a pain right? With this code we no longer need to define a new instance of this class each time, we can simply just access our_new_instance_of_a_class as and when we please.

Doubles

You may commonly hear people refer to doubles when unit testing and this can be somewhat misleading as a mock is a type of double, a fake or dummy is a type of double, a stub is a double. A double is quite simply a generic term for these kinds of tests, not a particular thing in itself.

Mocks

When using mocks we are testing expected behavior of a program and appropriately for these kinds of tests we use the keyword “expect”. For example, say we are expecting a class to contain a particular method, we could write;

expect(subject).to respond_to (:our_method_name)

As you can see RSpec tests are written pretty close to plain old English, we expect subject (our class) to respond to our method (:our_method_name).

Stubs

Stubs are used to test methods that could potentially have multiple outcomes, for example, something like a number or password generator. Hard coding test values in these scenarios would no doubt have undesirable outcomes, lets give an example in RSpec as illustrated in the video below.

Here we have a class ‘NumberGenerator’ and within the class, a method (random) that generates a random number and prints ‘A’ that many times. In the test itself, we create a new instance of the class named ‘generator’, however, the real magic of a stub happens following this. The code “allow(generator).to receive (:rand).and_return(5)” specifically tackles the random functionality of the method. In plain English, we are saying “when calling the random method, the built-in ruby method rand within this should always return 5 in the context of this test”. So in summary stubs are used to control values within a test.

Please feel free to check out this very informative video if these concepts still seem vague.

Thanks for reading I’m hoping soon to do some TDD with some Java code, so expect some Jtest code to appear on here in the near future!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Steven Klavins
Steven Klavins

Written by Steven Klavins

Hi, I’m Steven, most call me Steve! I’m a programmer, musician, and artist. This blog contains various tutorials and posts related to software development.

Responses (1)

Write a response

Thank you so much! I just started learning RSpec and this article really helped clarify the differences and uses of stubs and mocks

--