Laurie Young
Laurie Young

Reputation: 138494

Setting env when using rspec to test omniauth callbacks

I'm having a strange problem when trying to set a callback for Facebook Authentication via Omniauth. In my controller (simplified to just the code necessary to show the error) I have:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    raise env.inspect
    # auth_hash = env["omniauth.auth"]
  end
end

this works in production mode, showing me the hash. However in test mode env is set to nil.

I have the following set in my spec_helper.rb file

OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:facebook, {"credentials" => {
                                        "token" => "foo-token"
                                        }
                                     })

and my spec looks like this:

require 'spec_helper'

describe Users::OmniauthCallbacksController do

  describe "Facebook" do

    before(:each) do
      request.env["devise.mapping"] = Devise.mappings[:user] 
      request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
    end

    it "should be a redirect" do
      get :facebook
      response.should redirect_to(root_path)
    end

  end

end

Can anyone enlighten me on what I need to do to have env not be nil when running my tests?

Upvotes: 3

Views: 1469

Answers (1)

Joe Harris
Joe Harris

Reputation: 14045

I use the following in my spec_helper.rb :

RACK_ENV = ENV['ENVIRONMENT'] ||= 'test'

I don't use Rails or Devise though so YMMV. I've also seen various threads saying that someone had to do this before their requires to get it to work.

Upvotes: 1

Related Questions