简体   繁体   中英

How should I test Omniauth with Rspec and Capybara?

Trying to test OmniAuth with RSpec and Capybara, utterly failing.

So far, spec_helper.rb has:

# 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'
                                                            }
                                                        })

And I know I need to put Capybara tests under spec/features . So I have:

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

But I get:

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)>'

Utterly, utterly lost. Went through the OmniAuth wiki and it really didn't help; searched for over an hour through Stack Overflow, no luck. Help?

After doing quite a bit of reading, I finally managed to fix this. Test now reads like this:

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

Simple!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM