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.

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?


Need Your Help

How to search a specific pattern with commas and digits using egrep

regex unix grep

I have million plus text file containing lines similar to below

extract NSTimeZone from string like '+-hh:mm'

ios nsdate nsdateformatter nstimezone

In my app I download some data as JSON from an API which contains an entry with local time: