How to get rid of uninitialized constant Omniauth

2019-08-08 23:02发布

问题:

I have a fresh rails-composer omniauth application. When running tests with rspec it gives this error:

$ rspec
/home/grzegorz/programing/integra/spec/support/helpers.rb:5:in `block in <top (required)>': uninitialized constant Omniauth (NameError)
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/gems/rspec-core-3.3.1/lib/rspec/core.rb:97:in `configure'
from /home/grzegorz/programing/integra/spec/support/helpers.rb:4:in `<top (required)>'
from /home/grzegorz/programing/integra/spec/rails_helper.rb:23:in `block in <top (required)>'
from /home/grzegorz/programing/integra/spec/rails_helper.rb:23:in `each'
from /home/grzegorz/programing/integra/spec/rails_helper.rb:23:in `<top (required)>'
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/gems/rspec-core-3.3.1/lib/rspec/core/configuration.rb:1280:in `require'
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/gems/rspec-core-3.3.1/lib/rspec/core/configuration.rb:1280:in `block in requires='
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/gems/rspec-core-3.3.1/lib/rspec/core/configuration.rb:1280:in `each'
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/gems/rspec-core-3.3.1/lib/rspec/core/configuration.rb:1280:in `requires='
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/gems/rspec-core-3.3.1/lib/rspec/core/configuration_options.rb:109:in `block in process_options_into'
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/gems/rspec-core-3.3.1/lib/rspec/core/configuration_options.rb:108:in `each'
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/gems/rspec-core-3.3.1/lib/rspec/core/configuration_options.rb:108:in `process_options_into'
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/gems/rspec-core-3.3.1/lib/rspec/core/configuration_options.rb:21:in `configure'
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/gems/rspec-core-3.3.1/lib/rspec/core/runner.rb:101:in `setup'
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/gems/rspec-core-3.3.1/lib/rspec/core/runner.rb:88:in `run'
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/gems/rspec-core-3.3.1/lib/rspec/core/runner.rb:73:in `run'
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/gems/rspec-core-3.3.1/lib/rspec/core/runner.rb:41:in `invoke'
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/gems/rspec-core-3.3.1/exe/rspec:4:in `<top (required)>'
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/bin/rspec:23:in `load'
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/bin/rspec:23:in `<main>'
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/bin/ruby_executable_hooks:15:in `eval'
from /home/grzegorz/.rvm/gems/ruby-2.2.2@integra/bin/ruby_executable_hooks:15:in `<main>'

It gives me much trouble to debbug.

EDIT. My gem file has those:

gem 'omniauth'
gem 'omniauth-facebook'

It is a fresh app generated with rails-composer, no custom code.

回答1:

You should add require_relative 'helpers/omniauth' at the beginning of spec/support/helpers.rb - works for me.



回答2:

I was having the same error, and had to include the line

require 'omniauth'

in the beginning of the specs/spec_helper.rb file to get it working.



回答3:

I believe OmniAuth class is with capital "A". If it's not that, check your GemFile if you added the gem properly.



回答4:

omniauth gem works with other gems i.e if you want to use facebook,twitter you need to add other gems too.

  gem 'omniauth'
  gem 'omniauth-twitter'
  gem 'omniauth-facebook'