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

Already on GitHub? Sign in to your account

Helper tests mix the helper module into the example. #811

Closed
jamesotron opened this Issue Aug 23, 2013 · 2 comments

Comments

Projects
None yet
3 participants

This morning I discovered that when testing Rails helpers rspec-rails appears to mix the helper module into the example context.

I've verified this by creating a new empty rails 4.0 project using rails new foo, adding rspec-rails to the Gemfile and then running rails generate rspec:install.

I then created the following files:

app/helpers/application_helper.rb:

module ApplicationHelper
  def example_method
    :EXAMPLE
  end
end

spec/helpers/application_helper_spec.rb:

require 'spec_helper'

describe ApplicationHelper do
  specify { expect(method(:example_method)).not_to be }
  specify { expect(method(:example_method).source_location).not_to be}
end

When running this spec with bundle exec rspec spec/helpers/application_helper_spec.rb I get the following output:

FF

Failures:

  1) ApplicationHelper should not be
     Failure/Error: specify { expect(method(:example_method)).not_to be }
       expected #<Method: RSpec::Core::ExampleGroup::Nested_1(ApplicationHelper)#example_method> to evaluate to false
     # ./spec/helpers/application_helper_spec.rb:4:in `block (2 levels) in <top (required)>'

  2) ApplicationHelper should not be
     Failure/Error: specify { expect(method(:example_method).source_location).not_to be}
       expected ["/Users/jnh/Dev/tmp/foo/app/helpers/application_helper.rb", 2] to evaluate to false
     # ./spec/helpers/application_helper_spec.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.0052 seconds
2 examples, 2 failures

Failed examples:

rspec ./spec/helpers/application_helper_spec.rb:4 # ApplicationHelper should not be
rspec ./spec/helpers/application_helper_spec.rb:5 # ApplicationHelper should not be

Randomized with seed 56874

This is extremely confusing behaviour, because I was trying to test a helper which defined a user accessor, and write the following spec:

describe UserNotificationsHelper do
  let(:user) { double(:user, confirmation_token: :foo) }

  describe '#confirmation_token' do
     subject { helper.confirmation_token }
     before { assign(:user, user) }

     it { should eq user.confirmation_token }
  end
end

This test failed in an unexpected way by attempting to call confirmation_token on nil - because UserNotificationHelper's user method shadows the use of let.

vrybas commented May 26, 2014

@jamesotron looks like this issue is valid for v3.0.0.beta2, but on
v3.0.0.rc1 it gives me

1) ApplicationHelper
     Failure/Error: specify { expect(method(:example_method)).not_to be }
     NameError:
       undefined method `example_method' for class `RSpec::ExampleGroups::ApplicationHelper_2'
     # ./spec/helpers/application_helper_spec.rb:4:in `method'
     # ./spec/helpers/application_helper_spec.rb:4:in `block (2 levels) in <top (required)>'

which is expected. Can you confirm the issue is gone on v3.0.0.rc1?

Member

cupakromer commented Jun 1, 2014

This no longer seems to be an issue with RSpec 3. If it is, please let me know and include your repo steps and I'm happy to look into it further.

@cupakromer cupakromer closed this Jun 1, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment