我目前正在尝试将Spotify Web API集成到我的Ruby on Rails应用程序中。目前我正在尝试使用Capybara通过登录程序指导我的功能测试。我的测试目前看起来像这样:
require 'rails_helper'
feature 'visiting homepage' do
it 'Allows the user to log on with Spotify' do
visit('/')
click_on('Sign in with Spotify')
# expect(page).to have_content("Type in an artist, genre or mood and we'll do the rest")
end
end
意识到我需要模拟omniauth的身份验证流程,我遵循本教程的建议(https://github.com/omniauth/omniauth/wiki/Integration-Testing),这建议我将OmniAuth.config.test_mode = true
添加到我的测试中,以便“所有请求OmniAuth将被短路以使用模拟身份验证哈希,如下所述。对/ auth / provider的请求将立即重定向到/ auth / provider / callback。“所以我把这一行放在rails_helper.rb
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/rails'
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:spotify] = OmniAuth::AuthHash.new({
:provider => 'spotify',
:uid => '123545'
})
然后,我将重定向路由添加到Spotify Web API应用程序仪表板上的设置,并在config/routes.rb
get '/auth/spotify/callback'
然而,完成所有这些后,我收到以下错误:
Failures:
1) visiting homepage Allows the user to log on with Spotify
Failure/Error: click_on('Sign in with Spotify')
ActionController::RoutingError:
uninitialized constant Auth
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/omniauth-1.8.1/lib/omniauth/strategy.rb:415:in `call_app!'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/omniauth-1.8.1/lib/omniauth/strategy.rb:302:in `mock_callback_call'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/omniauth-1.8.1/lib/omniauth/strategy.rb:270:in `mock_call!'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/omniauth-1.8.1/lib/omniauth/strategy.rb:185:in `call!'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/omniauth-1.8.1/lib/omniauth/strategy.rb:168:in `call'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/omniauth-1.8.1/lib/omniauth/builder.rb:63:in `call'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/rack-2.0.4/lib/rack/etag.rb:25:in `call'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/rack-2.0.4/lib/rack/conditional_get.rb:25:in `call'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/rack-2.0.4/lib/rack/head.rb:12:in `call'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/rack-2.0.4/lib/rack/session/abstract/id.rb:232:in `context'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/rack-2.0.4/lib/rack/session/abstract/id.rb:226:in `call'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/railties-5.1.6/lib/rails/rack/logger.rb:36:in `call_app'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/railties-5.1.6/lib/rails/rack/logger.rb:24:in `block in call'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/railties-5.1.6/lib/rails/rack/logger.rb:24:in `call'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/rack-2.0.4/lib/rack/method_override.rb:22:in `call'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/rack-2.0.4/lib/rack/runtime.rb:22:in `call'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/rack-2.0.4/lib/rack/sendfile.rb:111:in `call'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/railties-5.1.6/lib/rails/engine.rb:522:in `call'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/rack-2.0.4/lib/rack/urlmap.rb:68:in `block in call'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/rack-2.0.4/lib/rack/urlmap.rb:53:in `each'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/rack-2.0.4/lib/rack/urlmap.rb:53:in `call'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/rack-test-1.0.0/lib/rack/mock_session.rb:29:in `request'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/rack-test-1.0.0/lib/rack/test.rb:259:in `process_request'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/rack-test-1.0.0/lib/rack/test.rb:129:in `custom_request'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/rack-test-1.0.0/lib/rack/test.rb:58:in `get'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/capybara-2.18.0/lib/capybara/rack_test/browser.rb:69:in `process'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/capybara-2.18.0/lib/capybara/rack_test/browser.rb:44:in `block in process_and_follow_redirects'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/capybara-2.18.0/lib/capybara/rack_test/browser.rb:43:in `times'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/capybara-2.18.0/lib/capybara/rack_test/browser.rb:43:in `process_and_follow_redirects'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/capybara-2.18.0/lib/capybara/rack_test/browser.rb:37:in `follow'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/capybara-2.18.0/lib/capybara/rack_test/node.rb:60:in `click'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/capybara-2.18.0/lib/capybara/node/element.rb:143:in `block in click'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/capybara-2.18.0/lib/capybara/node/base.rb:85:in `synchronize'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/capybara-2.18.0/lib/capybara/node/element.rb:143:in `click'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/capybara-2.18.0/lib/capybara/node/actions.rb:25:in `click_link_or_button'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/capybara-2.18.0/lib/capybara/session.rb:808:in `block (2 levels) in <class:Session>'
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/capybara-2.18.0/lib/capybara/dsl.rb:50:in `block (2 levels) in <module:DSL>'
# ./spec/features/visiting_homepage_spec.rb:6:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# NameError:
# uninitialized constant Auth
# /Users/lw9905/.rvm/gems/ruby-2.5.0/gems/omniauth-1.8.1/lib/omniauth/strategy.rb:415:in `call_app!'
Finished in 0.21256 seconds (files took 5.88 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/features/visiting_homepage_spec.rb:4 # visiting homepage Allows the user to log on with Spotify
我试图阅读并理解omniauth的strategy.rb
中的代码,但是,我找不到错误提及的未定义Auth
变量。