简体   繁体   English

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

[英]Rails: RSpec - undefined method `cookie_jar' for nil:NilClass

Rails newbie. Rails新手。 Trying to follow Michael Hartl's tutorial. 尝试遵循Michael Hartl的教程。

Stuck trying to add a helper method to simulate log in an RSpec test: 卡住尝试添加一个辅助方法来模拟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

In my spec/support/utilities.rb: 在我的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)>'

RSpec is very particular about the directory that you place your tests. RSpec对于放置测试的目录非常特别。 If you put the test in the wrong directory, it won't automagically mix-in the various test helpers that setup different types of tests. 如果将测试放在错误的目录中,它将不会自动地混入设置不同类型测试的各种测试助手。 It seems your setup is using spec/features which is not an approved default directory ( spec/requests , spec/integration , or spec/api ). 看来您的设置使用的是spec/features ,而不是经过批准的默认目录spec/requestsspec/integrationspec/api )。

Based on the tutorial page, I'm not sure how they have the spec_helper.rb file setup. 根据教程页面,我不确定他们如何设置spec_helper.rb文件。 Though the examples so they are using spec/requests to hold the tests. 通过示例,他们使用spec/requests来保存测试。

You can force RSpec to recognize another directory for request specs by using on of the following: 您可以使用以下选项之一,强制RSpec识别另一个目录来满足请求规范:

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: 将此包含在您的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

Since this is a tutorial I'd recommend following the standard of including require 'spec_helper' at the top of the spec file and that your actual spec/spec_helper.rb file has require 'rspec/rails' 因为这是一个教程,所以我建议遵循以下规范:在spec文件的顶部包含require 'spec_helper' ,并且您的实际spec/spec_helper.rb文件必须包含require 'rspec/rails'

A minor note, you don't need to put a specify inside of an it block. 一个小注释,您不需要在it块内放置一个specify They are aliases of each other, so just use one. 它们是彼此的别名,因此只需使用一个即可。

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, according to the documentation for capybara , you should be able to put your capybara tests into spec/features . 请注意,根据有关水豚的文档,您应该能够将水豚测试放入spec/features To make this work, ensure you are loading require 'capybara/rspec' in your spec_helper or test spec file directly. 为了使此工作有效,请确保在您的spec_helper或直接测试规范文件中加载require 'capybara/rspec' spec_helper require 'capybara/rspec'

However, looking at the source , I didn't see where they are automatically including that directory. 但是,从源代码来看,我没有看到它们自动包含该目录的位置。 You can also try adding the tag type: :feature to the outer describe block in your test file. 您还可以尝试在测试文件的外部describe块中添加标记type: :feature Though the more likely solution is to use spec/requests . 尽管更可能的解决方案是使用spec/requests

Shouldn't you have the "user" argument of the method enclosed in parenthesis? 您是否不应该将括号内的方法的“用户”参数括起来?

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

To have a mock cookie jar, you must have either rack-test or rspec-rails gem included in your Gemfile . 要使用模拟饼干罐,您的Gemfile必须包含rack-testrspec-rails gem。 I think maybe you have included just rspec and maybe missed out rspec-rails . 我认为您可能只包括了rspec ,也许错过了rspec-rails

You also need to ensure you've configured the session store as follows: 您还需要确保已按照以下方式配置了会话存储:

config.session_store = :cookie_store

This must have been done in either config/application.rb or some file under config/initializers . 这必须在config/application.rbconfig/initializers下的某个文件中完成。 If you have configured this in config/environments/development.rb or somewhere else, the Test environment will not be able to pick it up. 如果您在config/environments/development.rb或其他地方配置了此功能,则测试环境将无法对其进行提取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未定义的方法&#39;cookie_jar&#39;用于nil:NilClass用于sign_in Rspec帮助器 - Undefined method `cookie_jar' for nil:NilClass for sign_in Rspec helper RSpec Rails中nil:NilClass的未定义方法&#39;session&#39; - undefined method `session' for nil:NilClass in RSpec Rails RSpec中nil:NilClass的未定义方法 - undefined method for nil:NilClass in RSpec RSPEC对nil:NilClass的未定义方法“ jobs”(红宝石在轨道上) - RSPEC undefined method 'jobs' for nil:NilClass (ruby on rails) 为什么nil:NilClass的rspec rails失败“未定义方法`destroy” - why is this failing 'undefined method `destroy' for nil:NilClass' rspec rails Rails RSpec测试:nil的未定义方法&#39;deliver&#39;:NilClass - Rails RSpec Testing: Undefined method 'deliver' for nil:NilClass Rails 控制器 RSpec 未定义方法 `encode&#39; for nil:NilClass - Rails controller RSpec undefined method `encode' for nil:NilClass 带Rails 4.2的rspec的nil:NilClass的未定义方法`commit_records&#39; - rspec with Rails 4.2 undefined method `commit_records' for nil:NilClass Rails Rspec-nil:NilClass的模型属性未定义方法“&gt;” - Rails Rspec - model attribute undefined method `>' for nil:NilClass Rspec/Rails3:NoMethodError:nil:NilClass 的未定义方法“文本” - Rspec/Rails3: NoMethodError: undefined method `text' for nil:NilClass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM