Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 558
Laurie Young Avatar asked Sep 04 '26 16:09

Laurie Young


1 Answers

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.

like image 78
Joe Harris Avatar answered Sep 07 '26 09:09

Joe Harris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!