Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 299
mehulkar Avatar asked Mar 16 '12 02:03

mehulkar


2 Answers

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?

like image 105
Chris Avatar answered Sep 23 '22 22:09

Chris


Did you try moving these two lines to spec_helper.rb?

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

Also add the following before block in your test file:

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

You can find more info on this link: https://github.com/intridea/omniauth/wiki/Integration-Testing

like image 40
Miro Avatar answered Sep 21 '22 22:09

Miro