Rails:RSpec - 用于nil的未定义方法`cookie_jar':NilClass

时间:2013-05-02 20:45:15

标签: ruby-on-rails ruby authentication rspec capybara

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 <top (required)>'

3 个答案:

答案 0 :(得分:3)

RSpec非常关注您放置测试的目录。如果将测试放在错误的目录中,它将不会自动混合设置不同类型测试的各种测试助手。您的设置似乎使用的是spec/features,但未经批准default directory 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

由于这是一个教程,我建议您遵循在规范文件顶部添加require 'spec_helper'的标准,并确保您的实际spec/spec_helper.rb文件具有require 'rspec/rails'

一个小注,您不需要在specify块内放置it。它们是彼此的别名,所以只需使用一个。

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

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

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

答案 1 :(得分:0)

您是否应该在括号中包含方法的“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

答案 2 :(得分:-1)

要拥有模拟Cookie jar,您的rack-test中必须包含rspec-railsGemfile个gem。我想也许你只包括rspec,可能错过了rspec-rails

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

config.session_store = :cookie_store

这必须在config/application.rbconfig/initializers下的某个文件中完成。如果您已在config/environments/development.rb或其他地方配置了此功能,则测试环境将无法将其选中。