Skip to content

Advise using verify_doubled_constant_names #106

@pirj

Description

@pirj

In addition to verify_partial_doubles the above option may come handy to verify that you are stubbing an existing constant.

      # When this is set to true, an error will be raised when
      # `instance_double` or `class_double` is given the name of an undefined
      # constant. You probably only want to set this when running your entire
      # test suite, with all production code loaded. Setting this for an
      # isolated unit test will prevent you from being able to isolate it!

Example

With this option set to false the following example will, surprisingly, pass:

describe UserGreeter do
  it 'picks user name and prints greeting' do
    user = instance_double('Usor') # notice a typo
    allow(user).to receive(:name).and_return('John')
    expect { subject.process(user) }.to output('Hello, John')
end

When User renames its name method to full_name, this spec will still pass, but UserGreeter will blow up in production with NoMethodError.

Real-life Examples

Example violations (resulting with errors when the option is set to true):

# Typo: notice the leading space
let(:api) { instance_double(' ThirdParty::API') }

# `instance_double` expects a class name
let(:charge) { instance_double('payment', id: 1) } # "payment" is not a defined constant. Perhaps you misspelt it?

# Does not exist outside of the test code
let(:action) { instance_double('Action') } # "Action" is not a defined constant (though "Namespace::Action" is)

# Not loaded
let(:settings) { class_double('Settings') } # "Settings" is not a defined constant

# Double stubbing
before do
  stub_const('ActionClass', Class.new)
end
let(:action) { instance_double(ActionClass) } "ActionClass" is not a defined constant

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions