Inline setup versus let in RSpec

let has a long history in RSpec having been introduced with a commit in 2009 and rolled out as part of RSpec 2. I adopted it early on as the aesthetics appealed to me with let variables being defined first in little blocks with symbols. One of our engineers wrote a macro in vim to quickly hoist instance variables in older style RSpec tests to let declarations. It became the default style for everyone in RSpec with only a bit of controversy around let! which could be easy to miss in tests. I knew about the lazy initialization, but it was the aesthetics I prized.

I remember a bit of a community debate a few years later with a famous Thoughtbot blog post entitled Let’s Not. It argued pretty convincingly that let was a mystery guest pattern which you don’t want when writng clear tests. Since our team’s default at that point was to use let, but we wrote pretty small Sandi Metz style classes with a single responsibility, the lets weren’t causing a big headache. If the code and the specs were visible in a single editor window, it was easy to see what was going on.

Fast forward to working on one of the largest Ruby codebases in the world and I got reintroduced to the idea of containing the entire test inline. Much of the codebase doesn’t fit in a single editor window and suddenly those little lets are screens away and hard to find. Add with nested contexts, shared examples and the like I was really hating let. Some of the more painful specs potentially executed up to 100 lets before running an individual spec and often fired hundreds of SQL queries. I continued to use it on newer refactored code, but eventually the team started to cut back to allowing fewer lets in a given spec as a compromise. Today my default is to write the entire context of the test inline with no lets at all.

So a let heavy spec like the following is forced into:

RSpec.describe BlogPost do
  let(:author) { Author.new(name: "Gunther Hemingway") }
  let(:editor) { Editor.new(name: "Jordon Adams") }
  let(:category) { Category.new(name: "technology") }
  let(:comments) do
    [
      Comment.new(author: "hank", content: "great post!"),
      Comment.new(author: "lisa", content: "very informative."),
    ]
  end
  let(:tags) { ["ruby", "software development", "tdd"] }
  let(:blog_post) do
    BlogPost.new(
      title: "rspec and let",
      content: "using let in rspec can help with...",
      author: author,
      editor: editor,
      category: category,
      comments: comments,
      tags: tags
    )
  end

  describe "#publish" do
    it "notifies the author and editor when published" do
      blog_post.publish

      expect(author.notifications.last).to eq("your post 'rspec and let' has been published.")
      expect(editor.notifications.last).to eq("the post 'rspec and let' you edited has been published.")
    end
  end
end

This much more explict style:

RSpec.describe BlogPost do
  describe "#publish" do
    it "notifies the author and editor when published" do
      blog_post = BlogPost.new(
        title: "rspec and let",
        content: "using let in rspec can help with...",
        author: Author.new(name: "Gunther Hemingway")
        editor: Editor.new(name: "Jordon Adams")
        category: category.new(name: "technology"),
        comments: [
          comment.new(author: "leslie", content: "great post!"),
          comment.new(author: "bob", content: "very informative.")
        ],
        tags: ["ruby", "software development", "tdd"]
      )

      blog_post.publish

      expect(blog_post.author.notifications.last).to eq("your post 'rspec and let' has been published.")
      expect(blog_post.editor.notifications.last).to eq("the post 'rspec and let' you edited has been published.")
    end
  end
end

Inline default leads to several happy impacts:

  • If you ever need to move this spec, copy it as a starting point, etc, it’s all intact as a single unit
  • Sure you’ll probably duplicate some of this for a second spec, but deep in a spec file you’ll never need to scroll around to see what’s going on
  • If your setup code looks like many lines of boilerplate perhaps your class has way to many dependencies and needs reactoring

So if you haven’t tried this recently I’d invite you to do an experiment for a week and write zero lets.

Interview Tip: Checkout Online Code Environments before the Interview

Live coding is a stressful experience. One of the simplest ways to reduce stress around solving a unknown problem in less than an hour is to setup your environment before the interview. My anecdotal experience is that so few developers do this in practice. As an interviewer its a good sign when you check the coding environment before and see the candidate has already setup the environment and got a test up and running.

It’s a signal to the interviewer of several things in the interview:

  • You are naturally prepared
  • You are able to take full advantage of any tools/libraries provided
  • You are pretty motivated to work for this organization

Using CoderPad as an example:

  • You can login ahead of time into the pad setup for your interview or at least login to CoderPad’s site and try out a sandbox.
  • Select a few of the coding environments you are most comfortable with. Unless you are forced into a specific language always choose your day-to-day language.
  • See what libraries you have access to like RSpec, ActiveSupport in Ruby for example.
  • Look at the settings for including important things like vim/emac support, Intellisense, etc.
  • Get a test running in the environment, so you can TDD right away using your favorite testing framework or at least an available one.
  • Test drive a simple problem in the space like FizzBuzz just to get a feel for the environment and how it works.
  • If this is the actual coding environment for the interview leave your code in there for the interviewer to discover.

I know I see this level of preperation maybe 5-10% of the time, so it’s an easy way to start off strong.

Quick Inline Vim Keymap Shortcut

I often forget the exact syntax for writing a quick shortcut in in vim/neovim. After hunting for several minutes this morning I made a decision to memorialize the approach. Usually it’s for a simple command like running tests in an Elixir mix console:

:nnoremap ,e :!mix test<CR>

So the first n means it applies just to normal mode. The noremap means it won’t follow any other mapping so it’s non-recursive. The ,e is the what the shortcut is mapped to. And finally the :!mix test<CR> puts the mix command into the terminal and hits a carriage return to execute the command.

Aggregate Failures

After working on several large rails codebases in the last 10 years I’ve seen a familiar pattern. Many tests in Rails projects are integration tests because they rely on actual database objects existing. One assertion per test is great rule when you don’t have tens of thousands of specs running. :aggregate-failures allows you to have multiple assertions while still reporting on each failure clearly.

As a bonus it is honored by Rubocop RSpec RSpec/MultipleExpectations. Not sure why this isn’t documented better with Rubocop RSpec. Here is the code within the MultipleExpectations class that enforces the one assert per spec rule:

MultipleExpectations

def on_block(node)
  return unless example?(node)

  return if example_with_aggregate_failures?(node)

  expectations_count = to_enum(:find_expectation, node).count

  return if expectations_count <= max_expectations

  self.max = expectations_count

  flag_example(node, expectation_count: expectations_count)
end

And you don’t need to feel guilty about adding aggregate failures:

  • It speeds up test runs because it doesn’t do multiple setups
  • By default anything that uses ActiveRecord is not a true unit test
  • You still get all the errors if multiple lines fail
  • Speed of test run on any significant Rails project should almost always win

RSpec Instance Double With Class Names

After many years working with RSpec I discovered a nice little feature and a small gotcha with instance doubles. I’ve used instance_double since before it was ported from rspec-fire.

My practice and all the current Relish RSpec examples of instance_double use the following format:

let(:public_policy) { instance_double('Users::PublicPolicy') }

It turns out the actual instance_double() method takes a string representing the class or just the class constant:

instance_double(doubled_class)

And the important part here is the parameter:

doubled_class(String, Class)

No String is required here and class constants are auto-verifying. According to a long discussion on rspec-mocks this behavior exists to avoid some auto-loading of classes that aren’t needed so that tests can be a bit faster in some cases. For me this breaks the expectation that I the mock is actually verifying that the class and methods actually exist. If I wanted just a pure mock I could just use double. And for Rails projects that make up a lot of the day to day paid developer work everything auto-loads anyway. Using class constants is just simpler. If you’re on a legacy project you can probably just add the config to verify the strings ahead of time anyway with the following:

config.mock_with :rspec do |mocks|
  mocks.verify_doubled_constant_names = true
end

This example code shows what happens when you make a typo in the string constant name and you don’t have the config to verify set:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'spec_helper'
require_relative '../../lib/users/policy_enforcer'
require_relative '../../lib/users/public_policy'

RSpec.describe Users::PolicyEnforcer do
  describe '#allowed?' do
    let(:policy_enforcer) { Users::PolicyEnforcer.new(public_policy) }

    context 'with correct string instance_double class constant' do
      let(:public_policy) { instance_double('Users::PublicPolicy') }

      before do
        allow(public_policy).to receive(:allowed?)
      end

      it 'returns true' do
        policy_enforcer.allowed?
        expect(public_policy).to have_received(:allowed?)
      end
    end

    context 'with typo string instance_double class constant' do
      let(:public_policy) { instance_double('Use::PublicPolicy') }

      before do
        allow(public_policy).to receive(:allowed?)
      end

      it 'lies and returns true' do
        policy_enforcer.allowed?
        expect(public_policy).to have_received(:allowed?)
      end
    end

    context 'with a proper class constant instance_double' do
      let(:public_policy) { instance_double(Users::PublicPolicy) }

      before do
        allow(public_policy).to receive(:allowed?)
      end

      it 'returns true' do
        policy_enforcer.allowed?
        expect(public_policy).to have_received(:allowed?)
      end
    end

    context 'with an typoed class constant instance_double' do
      let(:public_policy) { instance_double(User:PublicPolicy) }

      before do
        allow(public_policy).to receive(:allowed?)
      end

      it 'fails because the constant is not defined' do
        policy_enforcer.allowed?
        expect(public_policy).to have_received(:allowed?)
      end
    end
  end
end

The result is the spec with typo string instance double class constant on line 22 lies to you. It behaves like a plain old double and allows you to accept methods on classes that don’t exist.

Users::PolicyEnforcer
  #allowed?
    with correct string instance_double class constant
      returns true
    with typo string instance_double class constant
      lies and returns true
    with a proper class constant instance_double
      returns true
    with an typoed class constant instance_double
      fails because the constant is not defined (FAILED - 1)

Failures:

  1) Users::PolicyEnforcer#allowed? with an typoed class constant instance_double fails because the constant isn not defined
     Failure/Error: let(:public_policy) { instance_double(User:PublicPolicy) }

     NameError:
       uninitialized constant PublicPolicy
     # ./spec/users/policy_enforcer_spec.rb:49:in `block (4 levels) in <top (required)>'
     # ./spec/users/policy_enforcer_spec.rb:52:in `block (4 levels) in <top (required)>'

Finished in 0.0175 seconds (files took 0.45833 seconds to load)
4 examples, 1 failure

Failed examples:

rspec ./spec/users/policy_enforcer_spec.rb:55 # Users::PolicyEnforcer#allowed? with an typoed class constant instance_double fails because the constant isn not
 defined