Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advise using verify_doubled_constant_names #106

Open
pirj opened this issue Aug 20, 2019 · 0 comments
Open

Advise using verify_doubled_constant_names #106

pirj opened this issue Aug 20, 2019 · 0 comments

Comments

@pirj
Copy link
Member

pirj commented Aug 20, 2019

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant