![](/img/trans.png)
[英]Undefined method `cookie_jar' for nil:NilClass for sign_in Rspec helper
[英]Rails: RSpec - undefined method `cookie_jar' for nil: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 <top (required)>'
RSpec對於放置測試的目錄非常特別。 如果將測試放在錯誤的目錄中,它將不會自動地混入設置不同類型測試的各種測試助手。 看來您的設置使用的是spec/features
,而不是經過批准的默認目錄 ( spec/requests
, spec/integration
或spec/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
或直接測試規范文件中加載require 'capybara/rspec'
spec_helper
require 'capybara/rspec'
。
但是,從源代碼來看,我沒有看到它們自動包含該目錄的位置。 您還可以嘗試在測試文件的外部describe
塊中添加標記type: :feature
。 盡管更可能的解決方案是使用spec/requests
。
您是否不應該將括號內的方法的“用戶”參數括起來?
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
要使用模擬餅干罐,您的Gemfile
必須包含rack-test
或rspec-rails
gem。 我認為您可能只包括了rspec
,也許錯過了rspec-rails
。
您還需要確保已按照以下方式配置了會話存儲:
config.session_store = :cookie_store
這必須在config/application.rb
或config/initializers
下的某個文件中完成。 如果您在config/environments/development.rb
或其他地方配置了此功能,則測試環境將無法對其進行提取。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.