Search code examples
ruby-on-rails-3rspecintegration-testingomniauthrspec-rails

Rails rspec and omniauth (integration testing)


My Rails 3.2 app uses OmniAuth and Devise to sign in with Twitter. The authentication system works fine. I would like to write an integration test in rspec to make sure everything works. Using the information in the wiki, I've written the following, but I know I'm missing things.

Under test.rb in config/environments, I have the following lines

OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:twitter] = {:provider => 'twitter', :uid => '123545'}

My rspec test looks like this:

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

Authentication is the name of my model and calling .uid in rails console returns the string fine.

I'm getting the following error when I run this test:

Failure/Error: Authentication.last.uid.should == '123545'
NoMethodError:
undefined method `uid' for nil:NilClass

Can anyone help me figure out how to use the OmniAuth mocks that are provided? An explanation for why and how it works would be appreciated as well.


Solution

  • I run into something similar.

    After changing my mock object from using symbol keys:

    OmniAuth.config.mock_auth[:twitter] = {
        :uid => '1337',
        :provider => 'twitter',
        :info => {
          :name => 'JonnieHallman'
        }
      }
    

    to using string keys:

    OmniAuth.config.mock_auth[:twitter] = {
        'uid' => '1337',
        'provider' => 'twitter',
        'info' => {
          'name' => 'JonnieHallman'
        }
      }
    

    it worked.

    And do you have

      request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter] 
    

    somewhere in your testcase?