Rails:RSpec – 用于nil的未定义方法`cookie_jar’:NilClass

Rails新手。 试着按照Michael Hartl的教程。

试图添加辅助方法来模拟RSpec测试中的日志:

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 

在我的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 

错误:

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

是什么赋予了?

编辑,完整堆栈跟踪:

 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 ' 

RSpec非常关注您放置测试的目录。 如果将测试放在错误的目录中,它将不会自动混合设置不同类型测试的各种测试助手。 您的设置似乎是使用spec/features ,这不是经过批准的默认目录 ( spec/requestsspec/integrationspec/api )。

根据教程页面,我不确定他们是如何设置spec_helper.rb文件的。 虽然这些例子是他们使用spec/requests来进行测试。

您可以通过使用以下内容强制RSpec识别请求规范的另一个目录:

手动将适当的模块添加到测试文件中:

 # 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 

将其包含在spec/spec_helper.rb文件中:

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

由于这是一个教程,我建议遵循在spec文件顶部包含require 'spec_helper'的标准,并且您的实际spec/spec_helper.rb文件require 'rspec/rails'

一个小注意事项,您不需要在it块中添加specify 。 它们是彼此的别名,所以只需使用一个。

 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 

请注意,根据水豚的文档,您应该能够将您的水豚测试放入spec/features 。 要使其工作,请确保在spec_helper或测试规范文件中直接加载spec_helper require 'capybara/rspec'

但是,查看源代码 ,我没有看到它们自动包含该目录的位置。 您还可以尝试将标记type: :feature :::添加到测试文件中的外部describe块。 虽然更可能的解决方案是使用spec/requests

你不应该用括号括起来的方法的“user”参数吗?

 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 

要拥有模拟cookie jar,您必须在Gemfile包含rack-testrspec-rails gem。 我想也许你只包括rspec并且可能错过了rspec-rails

您还需要确保已按如下方式配置会话存储:

config.session_store = :cookie_store

这必须在config/application.rbconfig/initializers下的某个文件中完成。 如果您已在config/environments/development.rb或其他位置配置此项,则测试环境将无法进行拾取。