简体   繁体   中英

Railsapp OAuth Tutorial, rspec “unitialized constant” error

Following the tutorial, I created a file omniauth.rb at the path

spec/support/helpers/omniauth.rb

module Omniauth

  module Mock
    def auth_mock
      OmniAuth.config.mock_auth[:twitter] = {
        'provider' => 'twitter',
        'uid' => '123545',
        'user_info' => {
          'name' => 'mockuser'
        },
        'credentials' => {
          'token' => 'mock_token',
          'secret' => 'mock_secret'
        }
      }
    end
  end

end

But when I run rspec, I get an error with "uninitialized constant Omniauth"

rails-omniauth/spec/support/helpers.rb:2:in `block in <top (required)>': uninitialized constant Omniauth (NameError)

It seems clear that either omniauth.rb or helpers.rb should be in a different location, but I don't know where.

Update :

I subsequently tried installing the rails-omniauth via the Rails Composer app. When I run "rspec" for this app, I get exactly the same error.

At one point in the tutorial you are given a choice between creating a file at at /spec/support/helpers.rb :

RSpec.configure do |config|
  config.include Omniauth::Mock
  config.include Omniauth::SessionHelpers, type: :feature
end
OmniAuth.config.test_mode = true

Or adding these same lines to /spec/rails_helper.rb .

I created the new file at /spec/support/helpers.rb . To make this work, I needed to add the line require_relative 'helpers/omniauth' at the top of the file. The Rails Composer app also adds the helpers.rb file rather than editing rails_helper.rb , so the same line is needed to make rspec run successfully for that app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM