加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 我应该如何使用Rspec和Capybara测试Omniauth?

发布时间:2020-12-17 02:33:09 所属栏目:百科 来源:网络整理
导读:试图用RSpec和Capybara测试OmniAuth,完全失败了. 到目前为止,spec_helper.rb有: # Enable omniauth testing modeOmniAuth.config.test_mode = trueOmniAuth.config.mock_auth[:google] = OmniAuth::AuthHash.new({ :provider = 'google',:uid = '1337',:inf
试图用RSpec和Capybara测试OmniAuth,完全失败了.

到目前为止,spec_helper.rb有:

# Enable omniauth testing mode
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:google] = OmniAuth::AuthHash.new({
                                                            :provider => 'google',:uid => '1337',:info => {
                                                                'name' => 'JonnieHallman','email' => 'jon@test.com'
                                                            }
                                                        })

而且我知道我需要将Capybara测试置于规格/功能之下.所以我有:

require 'spec_helper'
describe "Authentications" do
  context "without signing into app" do
    it "sign in button should lead to Google authentication page" do
      visit root_path
      click_link "Login"
      Authentication.last.uid.should == '1337'
    end
  end
end

但我得到:

1) Authentications without signing into app sign in button should lead to Google authentication page
 Failure/Error: Authentication.last.uid.should == '1337'
 NameError:
   uninitialized constant Authentication
 # ./spec/features/omniauth_spec.rb:10:in `block (3 levels) in <top (required)>'

完全失败了.通过OmniAuth wiki,它真的没有帮助;通过Stack Overflow搜索了一个多小时,没有运气.救命?

解决方法

在做了很多阅读之后,我终于设法解决了这个问题.测试现在看起来像这样:

require 'spec_helper'
describe "Authentications" do
  context "Clicking the login link" do
    it "Login button should log in" do
      visit root_path
      click_link "Login"
      page.should have_link "Logout"
    end
  end
end

简单!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读