Rails: RSpec - undefined cookie_jar method for nil: NilClass

Rails is a newbie. Trying to follow Michael Hartl’s textbook.

Stuck trying to add a helper method to simulate entering the RSpec test:

describe "when the a user has logged in and attempts to visit the page" do let(:user) { FactoryGirl.create :user } before do log_in user end it "should redirect the user to next page" do specify { response.should redirect_to loggedin_path } end end 

In my spec / support / utilities.rb:

 def log_in user visit root_path fill_in "Email", with: user.email fill_in "Password", with: user.password click_button "Log in" cookies[:remember_token] = user.remember_token end 

Error:

 Failure/Error: log_in user NoMethodError: undefined method `cookie_jar' for nil:NilClass 

What gives?

Edit, full stack trace:

 Index page when the a user has logged in and attempts to visit the page should redirect the user to next page Failure/Error: log_in user NoMethodError: undefined method `cookie_jar' for nil:NilClass # ./spec/support/utilities.rb:8:in `log_in' # ./spec/features/pages/index_spec.rb:20:in `block (3 levels) in <top (required)>' 
+4
source share
3 answers

RSpec is very specific in the directory in which you place your tests. If you put the test in the wrong directory, it will not automatically mix different test assistants that install different types of tests. It seems your installation uses spec/features , which is not an approved standard directory ( spec/requests , spec/integration or spec/api ).

Based on the tutorial page, I'm not sure how their spec_helper.rb file settings are spec_helper.rb . Although examples, so they use spec/requests for testing.

You can force RSpec to recognize another directory for query specifications using the following functions:

Manually add the proper module to the test file:

 # spec/features/pages/index_spec.rb require 'spec_helper' describe "Visiting the index page" do include RSpec::Rails::RequestExampleGroup # Rest of your test code context "when the a user has logged in and attempts to visit the page" do let(:user) { FactoryGirl.create :user } before do log_in user end specify { response.should redirect_to loggedin_path } end end 

Include this in your spec/spec_helper.rb file:

 RSpec::configure do |c| c.include RSpec::Rails::RequestExampleGroup, type: :request, example_group: { file_path: c.escaped_path(%w[spec (features)]) } end 

Since this is a tutorial, I would recommend following the require 'spec_helper' inclusion standard at the top of the spec file and that your actual spec/spec_helper.rb has require 'rspec/rails'

A quick note, you do not need to put specify inside the it block. They are aliases for each other, so just use them.

 context "when the a user has logged in and attempts to visit the page" do let(:user) { FactoryGirl.create :user } before do log_in user end # All of the following are the same it "redirects the user to next page" do response.should redirect_to loggedin_path end it { response.should redirect_to loggedin_path } specify "redirects the user to next page" do response.should redirect_to loggedin_path end specify { response.should redirect_to loggedin_path } end 

Note that as per the documentation for capybara, you can put your capybara tests in spec/features . To make this work, make sure you load require 'capybara/rspec' into your spec_helper or directly into the test file.

However, looking at the source , I did not see where they automatically include this directory. You can also try adding the type: :feature tag to the external describe block in the test file. Although the most likely solution is to use spec/requests .

+3
source

Don't you have an argument to the "user" method enclosed in parentheses?

 def log_in(user) visit root_path fill_in "Email", with: user.email fill_in "Password", with: user.password click_button "Log in" cookies[:remember_token] = user.remember_token end 
0
source

To have a fake cookie, your Gemfile must have the symbol rack-test or rspec-rails . I think maybe you only included rspec and maybe you skipped rspec-rails .

You also need to make sure that you have configured session storage as follows:

config.session_store = :cookie_store

This should be done either in config/application.rb or in the config/initializers file. If you configured this in config/environments/development.rb or elsewhere, the test environment will not be able to raise it.

0
source

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


All Articles