This is an adopted rails app with no tests. I am trying to test omniauth in an integration test but am getting an error (edit I have based upon this: https://github.com/intridea/omniauth/wiki/Integration-Testing). This reflects my lack of understanding of Rspec. It would seem that the request object would be available by default.
I have in my spec/spec_helper.rb:
config.include IntegrationSpecHelper, :type => :request
Capybara.default_host = 'http://localhost:3000'
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:facebook, {
:uid => '12345'
})
and in my spec/integration/login_spec:
require 'spec_helper'
describe ServicesController, "OmniAuth" do
before do
puts OmniAuth.config.mock_auth[:facebook]
puts request # comes back blank
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
end
it "sets a session variable to the OmniAuth auth hash" do
request.env["omniauth.auth"][:uid].should == '12345'
end
end
and I get the following error:
{“provider”=>”facebook”, “uid”=>”12345”, “user_info”=>{“name”=>”Bob
Example”}}F
Failures:
1) ServicesController OmniAuth sets a session variable to the
OmniAuth auth hash
Failure/Error: request.env[“omniauth.auth”] = OmniAuth.config.mock_auth[:facebook]
NoMethodError:
undefined methodenv' for nil:NilClassblock (2 levels) in ‘
# ./login_spec.rb:8:inFinished in 22.06 seconds 1 example, 1 failure
Failed examples:
rspec ./login_spec.rb:11 # ServicesController OmniAuth sets a session
variable to the OmniAuth auth hash
Should the request object be available here, by default? Does this error possibly mean something else?
thx
You’re getting
nilbecause you haven’t made any request yet.To make the test work, you have to do three things:
Here’s how I do it. First set up the mock in the
beforeblock, and then visit the URL corresponding to the provider (in this case facebook):From the wiki:
So you have to have a route which matches ‘/auth/:provider/callback’. Whatever action you map that do has to perform the stuff in step 3 above.
If you wanted to test that the session variable was set to the uid, you could do something like this (which works because you set the uid to ‘12345’ in the mock above):
And here’s a route and action that should make this pass:
routes.rb
controllers/sessions_controller.rb
That’s the gist of it. Hope that helps.