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

explicitly included helper methods not available (any more) with Rails 6.1 #2481

Open
robinboening opened this issue Mar 10, 2021 · 8 comments

Comments

@robinboening
Copy link

robinboening commented Mar 10, 2021

What Ruby, Rails and RSpec versions are you using?

Ruby version: 2.7.1
Rails version: 6.1
RSpec-core version: 3.10.1
RSpec-rails version: 4.1.1

Observed behaviour

In a helper spec, when explicitly including methods as helper_method's into the current controller the methods are not available on the ActionView level.

Simplified example

# lib/configuration_methods.rb
module ConfigurationMethods
    extend ActiveSupport::Concern

    included do
      helper_method :bar
    end
end

# spec/helpers/foo_helper_spec.rb
before do
  helper.controller.class.include(ConfigurationMethods)
end

it 'foo bar' do
  expect(helper.bar).to eq("bar")
end

The example fails with

ActionView::Template::Error:
       undefined method `bar' for #<ActionView::Base:0x0000000000c0d0>

This is only a problem with Rails 6.1 as it doesn't fail with Rails 6.0.x.
I was able to track down what change in Rails is causing the issue

rails/rails@758ad13#diff-3149407bd2f6287e7791fc3e0cc5b2559d7e4e0c19eb1e7f7ea84e0db1e35ac5L33-R50

but I am not fully understanding the change nor do I know if rspec-rails needs to adapt to that change or if this change in rails is simply a bug.

Expected behaviour

bar should be available within ActionView; the example should succeed.

Can you provide an example app?

I will see if I can set up a demo app, if needed.

Let me know if I can provide more details.

Cheers
Robin

@benoittgt
Copy link
Member

Hello @robinboening

Could you try with main branch. I don't think we fixed this behavior but I want to be sure before trying to look at it.

Cheers

@robinboening
Copy link
Author

Thanks for the blazingly fast response, @benoittgt

I can confirm its failing using the current main branch.

I created a rails 6.1 app showing the failing example, using rspec-rails main branch from github: https://github.com/robinboening/rspec-issue-demo (the readme contains some info as well)

@benoittgt
Copy link
Member

Thanks @robinboening for the perfect repro app. I played a little bit with the code and Rails. The PR from @jhawthorn is very interesting. I was not able to find a solution after 1h.

I am wondering if we should not call explicitly a method to defined them helper method? @jhawthorn any idea?

@robinboening
Copy link
Author

Just pinging @jhawthorn again in case he missed it :)

@aergonaut
Copy link

I am also seeing this issue, but in a different context than what @robinboening reported. However, I think my issue is the same. In my case, I have a view spec that is testing a view which uses a helper method that is defined in the controller using helper_method. The view works correctly when I run the server, but when I run the view spec, I get a NameError.

This started appearing after upgrading to Rails 6.1 only.

I have created another repo to demonstrate the issue I am seeing: https://github.com/aergonaut/rspec-rails-helper-method-issue-demo

The demo.html.erb_spec.rb view spec fails on a NameError for the example_helper_method helper.

This method is defined in DemoController using helper_method.

When running the server, the helper method works as expected.

But in the view spec, the helper cannot be found. The output is this:

bundle exec rspec                                      
**F

Pending: (Failures listed here are expected and do not affect your suite's status)

  1) DemoHelper add some examples to (or delete) /Users/chris/src/coupa/rspec-rails-helper-method-issue-demo/spec/helpers/demo_helper_spec.rb
     # Not yet implemented
     # ./spec/helpers/demo_helper_spec.rb:14

  2) Demos GET /index add some examples (or delete) /Users/chris/src/coupa/rspec-rails-helper-method-issue-demo/spec/requests/demo_spec.rb
     # Not yet implemented
     # ./spec/requests/demo_spec.rb:5


Failures:

  1) demo/demo.html.erb demonstrates the issue
     Failure/Error: <h1><%= example_helper_method %></h1>
     
     ActionView::Template::Error:
       undefined local variable or method `example_helper_method' for #<ActionView::Base:0x00000000008160>
     # ./app/views/demo/demo.html.erb:1:in `_app_views_demo_demo_html_erb___545069705288937197_16540'
     # ./spec/views/demo/demo.html.erb_spec.rb:5:in `block (2 levels) in <main>'
     # ------------------
     # --- Caused by: ---
     # NameError:
     #   undefined local variable or method `example_helper_method' for #<ActionView::Base:0x00000000008160>
     #   ./app/views/demo/demo.html.erb:1:in `_app_views_demo_demo_html_erb___545069705288937197_16540'

Finished in 0.04022 seconds (files took 0.95446 seconds to load)
3 examples, 1 failure, 2 pending

Failed examples:

rspec ./spec/views/demo/demo.html.erb_spec.rb:4 # demo/demo.html.erb demonstrates the issue

@afdev82
Copy link

afdev82 commented Jun 10, 2021

Hi 👋 ,
any news about this issue?
I don't know if I can help out in some way, since I'm new to this project

@benoittgt
Copy link
Member

Hello

I am wondering if providing a reproducing template using https://github.com/rails/rails/tree/main/guides/bug_report_templates to see if the issue isn't on the rails side?

@benoittgt
Copy link
Member

There is an interesting answer from John Hawthorn here rails/rails#40204 (comment)

And also this PR rails/rails#40125

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

4 participants