rspec-rails: Rails 5: uninitialized constant ActionView::Template::Handlers::ERB::ENCODING_FLAG

I have the rails_helper.rb and the spec_helper.rb generated by the scaffolder. I have an empty controller test

require 'rails_helper'

RSpec.describe HomeController, type: :controller do
end

When I run either bundle exec rails spec or bundle exec rspec spec/controllers/home_controller_spec.rb I get

/home/me/.rvm/gems/ruby-2.3.1@global/gems/actionview-5.0.0/lib/action_view/template/handlers/erb.rb:90:in `<class:ERB>': uninitialized constant ActionView::Template::Handlers::ERB::ENCODING_FLAG (NameError)
Did you mean?  Encoding
    from /home/me/.rvm/gems/ruby-2.3.1@global/gems/actionview-5.0.0/lib/action_view/template/handlers/erb.rb:76:in `<module:Handlers>'
    from /home/me/.rvm/gems/ruby-2.3.1@global/gems/actionview-5.0.0/lib/action_view/template/handlers/erb.rb:5:in `<class:Template>'
    from /home/me/.rvm/gems/ruby-2.3.1@global/gems/actionview-5.0.0/lib/action_view/template/handlers/erb.rb:4:in `<module:ActionView>'
    from /home/me/.rvm/gems/ruby-2.3.1@global/gems/actionview-5.0.0/lib/action_view/template/handlers/erb.rb:3:in `<top (required)>'
    from /home/me/.rvm/gems/ruby-2.3.1@global/gems/actionview-5.0.0/lib/action_view/template/handlers.rb:12:in `require'
    from /home/me/.rvm/gems/ruby-2.3.1@global/gems/actionview-5.0.0/lib/action_view/template/handlers.rb:12:in `extended'
    from /home/me/.rvm/gems/ruby-2.3.1@global/gems/actionview-5.0.0/lib/action_view/template.rb:111:in `extend'
    from /home/me/.rvm/gems/ruby-2.3.1@global/gems/actionview-5.0.0/lib/action_view/template.rb:111:in `<class:Template>'
    from /home/me/.rvm/gems/ruby-2.3.1@global/gems/actionview-5.0.0/lib/action_view/template.rb:7:in `<module:ActionView>'
    from /home/me/.rvm/gems/ruby-2.3.1@global/gems/actionview-5.0.0/lib/action_view/template.rb:5:in `<top (required)>'
    from /home/me/.rvm/gems/ruby-2.3.1@global/gems/actionview-5.0.0/lib/action_view/template/resolver.rb:4:in `require'
    from /home/me/.rvm/gems/ruby-2.3.1@global/gems/actionview-5.0.0/lib/action_view/template/resolver.rb:4:in `<top (required)>'
    from /home/me/.rvm/gems/ruby-2.3.1@global/gems/actionview-5.0.0/lib/action_view/testing/resolvers.rb:1:in `require'
    from /home/me/.rvm/gems/ruby-2.3.1@global/gems/actionview-5.0.0/lib/action_view/testing/resolvers.rb:1:in `<top (required)>'
    from /home/me/.rvm/gems/ruby-2.3.1@my_app/gems/rspec-rails-3.5.1/lib/rspec/rails/view_rendering.rb:1:in `require'
    from /home/me/.rvm/gems/ruby-2.3.1@my_app/gems/rspec-rails-3.5.1/lib/rspec/rails/view_rendering.rb:1:in `<top (required)>'
    from /home/me/.rvm/gems/ruby-2.3.1@my_app/gems/rspec-rails-3.5.1/lib/rspec/rails.rb:9:in `require'
    from /home/me/.rvm/gems/ruby-2.3.1@my_app/gems/rspec-rails-3.5.1/lib/rspec/rails.rb:9:in `<top (required)>'
    from /home/me/my_app/my_app-web/spec/rails_helper.rb:2:in `require'
    from /home/me/my_app/my_app-web/spec/rails_helper.rb:2:in `<top (required)>'
    from /home/me/my_app/my_app-web/spec/controllers/home_controller_spec.rb:1:in `require'
    from /home/me/my_app/my_app-web/spec/controllers/home_controller_spec.rb:1:in `<top (required)>'
    from /home/me/.rvm/gems/ruby-2.3.1@my_app/gems/rspec-core-3.5.2/lib/rspec/core/configuration.rb:1435:in `load'
    from /home/me/.rvm/gems/ruby-2.3.1@my_app/gems/rspec-core-3.5.2/lib/rspec/core/configuration.rb:1435:in `block in load_spec_files'
    from /home/me/.rvm/gems/ruby-2.3.1@my_app/gems/rspec-core-3.5.2/lib/rspec/core/configuration.rb:1433:in `each'
    from /home/me/.rvm/gems/ruby-2.3.1@my_app/gems/rspec-core-3.5.2/lib/rspec/core/configuration.rb:1433:in `load_spec_files'
    from /home/me/.rvm/gems/ruby-2.3.1@my_app/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:100:in `setup'
    from /home/me/.rvm/gems/ruby-2.3.1@my_app/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:86:in `run'
    from /home/me/.rvm/gems/ruby-2.3.1@my_app/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:71:in `run'
    from /home/me/.rvm/gems/ruby-2.3.1@my_app/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:45:in `invoke'
    from /home/me/.rvm/gems/ruby-2.3.1@my_app/gems/rspec-core-3.5.2/exe/rspec:4:in `<top (required)>'
    from /home/me/.rvm/gems/ruby-2.3.1@my_app/bin/rspec:23:in `load'
    from /home/me/.rvm/gems/ruby-2.3.1@my_app/bin/rspec:23:in `<main>'
    from /home/me/.rvm/gems/ruby-2.3.1@my_app/bin/ruby_executable_hooks:15:in `eval'
    from /home/me/.rvm/gems/ruby-2.3.1@my_app/bin/ruby_executable_hooks:15:in `<main>'

The workaround is to add require "rails/all" at the beginning of rails_helper.rb or of spec_helper.rb but that wasn’t needed for Rails 4 and rspec-rails 3.1.

Did I miss something in the documentation?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 29 (10 by maintainers)

Commits related to this issue

Most upvoted comments

I got the same issue, but after I adding require File.expand_path("../../config/environment", __FILE__) to my spec_help.rb, and it works.

see http://stackoverflow.com/questions/4429068/getting-an-unitialized-constant-error-with-rspec-have-no-idea-whats-causing-it

I got around this by adding to my spec_helper.rb file

require File.expand_path("../../config/environment", __FILE__)

I came across this problem as well when creating a new Rails 5.1.1. app, with Rspec, Capybara and Factory Girl. Things worked once I placed expand_path at the top of the file.

My setup looks like:

require File.expand_path("../../config/environment", __FILE__)
require 'rails/all'
require 'capybara/rails'
require 'factory_girl_rails'

Interesting! Thanks for the heads-up, @johnpitchko

I fixed this (on a fresh Rails 5 app, haven’t even added test yet) by changing the .rspec file from

--require spec_helper

to

--require rails_helper

Does this seem ok to ppl?

Be aware that adding require File.expand_path('../../config/environment', __FILE__) causes rails to be booted essentially so should only be added to files where thats expected to be the case (which is not normally needed for plain old ruby specs).