【发布时间】:2012-03-16 02:07:03
【问题描述】:
My Rails 3.2 应用程序使用 OmniAuth 和 Devise 通过 Twitter 登录。身份验证系统工作正常。我想在 rspec 中编写一个集成测试以确保一切正常。使用 wiki 中的信息,我编写了以下内容,但我知道我遗漏了一些东西。
在 config/environments 中的 test.rb 下,我有以下几行
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:twitter] = {:provider => 'twitter', :uid => '123545'}
我的 rspec 测试如下所示:
describe "Authentications" do
context "without signing into app" do
it "twitter sign in button should lead to twitter authentication page" do
visit root_path
click_link "Sign in with Twitter"
Authentication.last.uid.should == '123545'
end
end
end
Authentication 是我的模型的名称,在 rails 控制台中调用 .uid 会返回正确的字符串。
我在运行此测试时收到以下错误:
Failure/Error: Authentication.last.uid.should == '123545'
NoMethodError:
undefined method `uid' for nil:NilClass
谁能帮我弄清楚如何使用提供的 OmniAuth 模拟?对于为什么和如何它的工作原理的解释也将不胜感激。
【问题讨论】:
标签: ruby-on-rails-3 rspec integration-testing omniauth rspec-rails