如何使用 Rails 和 minitest 模拟 OmniAuth 散列?
How do I mock an OmniAuth hash using Rails and minitest?
我正在使用 Rails 5 和 minitest。我想模拟登录到我的会话控制器,它依赖于 omniauth(我使用 Google 和 FB 进行登录)。我在我的控制器测试中有这个,test/controllers/rates_controller_test.rb,
class RatesControllerTest < ActionDispatch::IntegrationTest
# Login the user
def setup
logged_in_user = users(:one)
login_with_user(logged_in_user)
end
然后我尝试在我的测试助手中设置登录,test/test_helper.rb,
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
def setup_omniauth_mock(user)
OmniAuth.config.test_mode = true
omniauth_hash = { 'provider' => 'google',
'uid' => '12345',
'info' => {
'name' => "#{user.first_name} #{user.last_name}",
'email' => user.email,
},
'extra' => {'raw_info' =>
{ 'location' => 'San Francisco',
'gravatar_id' => '123456789'
}
}
}
OmniAuth.config.add_mock(:google, omniauth_hash)
end
# Add more helper methods to be used by all tests here...
def login_with_user(user)
setup_omniauth_mock(user)
post sessions_path
end
但是,当我 运行 我的控制器测试时,当在我的会话控制器中评估此行时,我得到一个 nil 值 ...
user = User.from_omniauth(env["omniauth.auth"])
以上,'env["omniauth.auth"]' 的计算结果为零。
When you try to test the OmniAuth, you need to set two env variables
并使用 RSpec
提供示例
before do
Rails.application.env_config["devise.mapping"] = Devise.mappings[:user] # If using Devise
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
end
在你的情况下,你可能需要设置
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google]
在您的 setup_omniauth_mock
方法中,在调用 OmniAuth.config.add_mock
之后。
我正在使用 Rails 5 和 minitest。我想模拟登录到我的会话控制器,它依赖于 omniauth(我使用 Google 和 FB 进行登录)。我在我的控制器测试中有这个,test/controllers/rates_controller_test.rb,
class RatesControllerTest < ActionDispatch::IntegrationTest
# Login the user
def setup
logged_in_user = users(:one)
login_with_user(logged_in_user)
end
然后我尝试在我的测试助手中设置登录,test/test_helper.rb,
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
def setup_omniauth_mock(user)
OmniAuth.config.test_mode = true
omniauth_hash = { 'provider' => 'google',
'uid' => '12345',
'info' => {
'name' => "#{user.first_name} #{user.last_name}",
'email' => user.email,
},
'extra' => {'raw_info' =>
{ 'location' => 'San Francisco',
'gravatar_id' => '123456789'
}
}
}
OmniAuth.config.add_mock(:google, omniauth_hash)
end
# Add more helper methods to be used by all tests here...
def login_with_user(user)
setup_omniauth_mock(user)
post sessions_path
end
但是,当我 运行 我的控制器测试时,当在我的会话控制器中评估此行时,我得到一个 nil 值 ...
user = User.from_omniauth(env["omniauth.auth"])
以上,'env["omniauth.auth"]' 的计算结果为零。
When you try to test the OmniAuth, you need to set two env variables
并使用 RSpec
提供示例before do
Rails.application.env_config["devise.mapping"] = Devise.mappings[:user] # If using Devise
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
end
在你的情况下,你可能需要设置
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:google]
在您的 setup_omniauth_mock
方法中,在调用 OmniAuth.config.add_mock
之后。