ruby - Rails: RSpec - undefined method `cookie_jar' for nil:NilClass -
rails newbie. trying follow michael hartl's tutorial.
stuck trying add helper method simulate log in rspec test:
describe "when user has logged in , attempts visit page" let(:user) { factorygirl.create :user } before log_in user end "should redirect user next page" specify { response.should redirect_to loggedin_path } end end
in 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' nil:nilclass
what gives?
edit, full stack trace:
index page when user has logged in , attempts visit page should redirect user next page failure/error: log_in user nomethoderror: undefined method `cookie_jar' nil:nilclass # ./spec/support/utilities.rb:8:in `log_in' # ./spec/features/pages/index_spec.rb:20:in `block (3 levels) in <top (required)>'
rspec particular directory place tests. if put test in wrong directory, won't automagically mix-in various test helpers setup different types of tests. seems setup using spec/features
not approved default directory (spec/requests
, spec/integration
, or spec/api
).
based on tutorial page, i'm not sure how have spec_helper.rb
file setup. though examples using spec/requests
hold tests.
you can force rspec recognize directory request specs using on of following:
manually add proper module test file:
# spec/features/pages/index_spec.rb require 'spec_helper' describe "visiting index page" include rspec::rails::requestexamplegroup # rest of test code context "when user has logged in , attempts visit page" let(:user) { factorygirl.create :user } before log_in user end specify { response.should redirect_to loggedin_path } end end
include in spec/spec_helper.rb
file:
rspec::configure |c| c.include rspec::rails::requestexamplegroup, type: :request, example_group: { file_path: c.escaped_path(%w[spec (features)]) } end
since tutorial i'd recommend following standard of including require 'spec_helper'
@ top of spec file , actual spec/spec_helper.rb
file has require 'rspec/rails'
a minor note, don't need put specify
inside of it
block. aliases of each other, use one.
context "when user has logged in , attempts visit page" let(:user) { factorygirl.create :user } before log_in user end # of following same "redirects user next page" response.should redirect_to loggedin_path end { response.should redirect_to loggedin_path } specify "redirects user next page" response.should redirect_to loggedin_path end specify { response.should redirect_to loggedin_path } end
note, according documentation capybara, should able put capybara tests spec/features
. make work, ensure loading require 'capybara/rspec'
in spec_helper
or test spec file directly.
however, looking @ source, didn't see automatically including directory. can try adding tag type: :feature
outer describe
block in test file. though more solution use spec/requests
.
Comments
Post a Comment