gpt4 book ai didi

ruby-on-rails - rails : RSpec - undefined method `cookie_jar' for nil:NilClass

转载 作者:数据小太阳 更新时间:2023-10-29 07:36:18 26 4
gpt4 key购买 nike

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,这不是经过批准的 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 block 中。它们互为别名,所以只用一个。

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 的文档,您应该能够将 capybara 测试放入 spec/features。要使其正常工作,请确保您在 spec_helper 中加载 require 'capybara/rspec' 或直接测试规范文件。

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

关于ruby-on-rails - rails : RSpec - undefined method `cookie_jar' for nil:NilClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16347410/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com