我该如何用Rspec和Capybara测试Omniauth?

时间:2012-12-11 13:12:30

标签: ruby-on-rails rspec capybara omniauth

尝试用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测试置于spec/features之下。所以我有:

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搜索了一个多小时,没有运气。帮助

1 个答案:

答案 0 :(得分:4)

经过相当多的阅读后,我终于设法解决了这个问题。现在测试如下:

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

简单!