Getting the undefined `env method for nil: NilClass" in an integration test for OmniAuth

I have an Omniauth testing mode:

spec_helper (I put it at the bottom of the file, right before end ):

 #Turn on "test mode" for OmniAuth OmniAuth.config.test_mode = true 

and this is my test:

specifications / request / authorization_pages_spec.rb:

  describe "signin" do before { visit signin_path } . . . describe "with OmniAuth" do before do OmniAuth.config.add_mock :facebook, uid: "fb-12345", info: { name: "Bob Smith" } visit root_path end describe "Facebook provider" do before do request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook] click_link "Sign in with Facebook" end it { should have_selector('title', text: user.name) } it { should have_link('Users', href: users_path) } it { should have_link('Profile', href: user_path(user)) } it { should have_link('Settings', href: edit_user_path(user)) } it { should have_link('Sign out', href: signout_path) } it { should_not have_link('Sign in', href: signin_path) } end 

when I run my test, I get the following:

Failures:

  1) Authentication signin with OmniAuth Facebook provider Failure/Error: request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook] NoMethodError: undefined method `env' for nil:NilClass # ./spec/requests/authentication_pages_spec.rb:57:in `block (5 levels) in <top (required)>' 

(etc.).

Is there something or am I doing something wrong?

+4
source share
1 answer

I think you are mixing two different things in this example. You are trying to write an integration specification, but the request AFAIK method is only available in controller specifications (for specifications inside the spec/controllers directory). Therefore, you have an undefined method env 'for the nil: NilClass` error.

In my sample project, you can find a useful OmniAuth integration specification: https://github.com/lucassus/locomotive/blob/9cd7dfd365469fc70fc367f29705a56df9730f6f/spec/features/user_facebook_sign_in_spec.rb

Hope this helps you.

+2
source

Source: https://habr.com/ru/post/1443623/


All Articles