我的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
身份验证是我的模型的名称,在rails控制台中调用.uid会返回字符串.
运行此测试时出现以下错误:
Failure/Error: Authentication.last.uid.should == '123545' NoMethodError: undefined method `uid' for nil:NilClass
任何人都可以帮我弄清楚如何使用提供的OmniAuth模拟?关于它为什么以及如何工作的解释也将受到赞赏.
解决方法
我碰到了类似的东西.
从使用符号键更改我的模拟对象后:
OmniAuth.config.mock_auth[:twitter] = { :uid => '1337',:provider => 'twitter',:info => { :name => 'JonnieHallman' } }
使用字符串键:
OmniAuth.config.mock_auth[:twitter] = { 'uid' => '1337','provider' => 'twitter','info' => { 'name' => 'JonnieHallman' } }
有效.
你有吗?
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
在你的测试用例的某个地方?
原文地址:https://www.jb51.cc/ruby/271222.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。