4

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

4

1 回答 1

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

简单的!

于 2012-12-12T16:33:31.347 回答