[英]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/requests
, spec/integration
或spec/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-test
或rspec-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.rb
或config/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.