我正在使用 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"]' 的计算结果为零。