Rspec-rails: Tests fail if there is a class named '*Helper' used in a model

Created on 3 Aug 2017  Â·  6Comments  Â·  Source: rspec/rspec-rails

I created a file named search_helper.rb inside the app/helpers folder.
It had a simple class inside, nothing fancy:
app/helpers/search_helper.rb:

class SearchHelper
    #some_code
end

It was used in a model:
app/models/end_user.rb:

search_helper = SearchHelper.new

After this change, when I ran the test cases, all of them failed with the following error(s):

An error occurred while loading ./spec/models/venue_spec.rb.
Failure/Error: require 'rspec/rails'

NameError:
uninitialized constant ActionView::TestCase::Behavior
# ./spec/rails_helper.rb:7:in `<top (required)>'
# ./spec/models/venue_spec.rb:1:in `<top (required)>'

When I renamed SearchHelper to SearchManager, the test cases started working again. Looks like Rspec had a problem with the word Helper.

My Versions:
Rails 5.0.4
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]
Mac OS Sierra
rspec-rails 3.6.0

All 6 comments

I got the same error and it seems your solution works. Has spent more than 1 hour to figure the correct way to do it but I'm still clueless. Thank you for the hack.

Rails expects the files in app/helpers to define Modules that are included in the view context. Using that directory differently is the root cause here.

It is unfortunate how this fails. I'm not sure what can be done to clarify the issue from rspec-rails.

@mikegee Oh! I never knew that the helpers folder had a specific purpose. Thanks for letting me know. I found this:

Rails “helpers” are very specifically view helpers. They’re automatically included in views, but not in controllers or models. That’s on purpose.

I also get this error while defining a helper method within a concern:

module Concerns
  module MyConcern
    extend ActiveSupport::Concern
     included do
       helper_method :my_method
     end
...

Throws uninitialized constant ActionView::TestCase::Behavior in tests

These are Rails issues, we don't do anything special here and the behaviour is being triggered by Rails sorry!

any update ?

Was this page helpful?
0 / 5 - 0 ratings