未定义方法“sign_in”对于#尝试使用 Devise 设置 RSpec 时出错
我有一个 spec/controllers/add_to_carts_spec.rb
:
require 'spec_helper'
describe CartItemsController do
before (:each) do
@user = Factory(:user)
sign_in @user
end
describe "add stuff to the cart" do
it "should add a product to the cart" do
product = FactoryGirl.create(:product)
visit products_path(product)
save_and_open_page
click_on('cart_item_submit')
end
end
end
和 /spec/support/spec_helper.rb
:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.use_transactional_fixtures = true
end
... 它还加载 /spec/support /devise.rb
:Guard
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
在后台运行并不断抛出此消息:
Failures:
1) CartItemsController add stuff to the cart should add a product to the cart
Failure/Error: sign_in @user
NoMethodError:
undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x1057fd428>
# ./spec/controllers/add_to_carts_spec.rb:7
我花了过去几个小时尝试各种配置调整和不同的语法,但似乎没有任何变化。有什么想法吗?
(编辑以反映新的错误)
I've got a spec/controllers/add_to_carts_spec.rb
:
require 'spec_helper'
describe CartItemsController do
before (:each) do
@user = Factory(:user)
sign_in @user
end
describe "add stuff to the cart" do
it "should add a product to the cart" do
product = FactoryGirl.create(:product)
visit products_path(product)
save_and_open_page
click_on('cart_item_submit')
end
end
end
and /spec/support/spec_helper.rb
:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.use_transactional_fixtures = true
end
... which also loads /spec/support/devise.rb
:
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
Guard is running in the background and keeps throwing this:
Failures:
1) CartItemsController add stuff to the cart should add a product to the cart
Failure/Error: sign_in @user
NoMethodError:
undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x1057fd428>
# ./spec/controllers/add_to_carts_spec.rb:7
I've spent the last couple hours trying various config adjustments and different syntaxes but nothing seems to change. Any ideas?
(edited to reflect newer error)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
理想的解决方案是在 spec/support/devise.rb 创建一个文件,并通过以下代码在 Rspec 配置中包含设计测试助手:
type 参数仅在控制器规格中包含助手,这是为了避免将来出现问题这可能是由于在测试模型或视图时调用它而引起的。这是可选的。
我们决定添加一个离散文件来包含帮助程序,而不是像上面 solnic 那样将它们包含在规范中,是因为如果重新生成规范,规范将被覆盖。
The ideal solution would be to create a file at spec/support/devise.rb and include the devise test helpers in the Rspec config through the following code:
The type parameter includes the helpers only in your controller specs, this is to avoid future issues that could arise from its invocation when testing models or views. It's optional.
The reason we decided to add a discrete file for including the helpers, as opposed to including them in the specs as solnic did above is because in the event that the specs are regenerated, the spec will be overwritten.
由于某种原因,这对我来说也不起作用,所以我只是手动将此助手包含在我的规范中,如下所示:
For some reason this also doesn't work for me so I just manually include this helper in my specs like this:
这些测试助手不适用于集成/请求规范。在这些情况下测试 Devise 的推荐方法是访问登录页面,填写表单并提交,然后运行测试。
请参阅 David Chelimsky 对之前有关此主题的问题的回答更完整的解释。
Those test helpers won't work for integration/request specs. The recommended way to test Devise in these situations is to visit the login page, fill in the form and submit it, and then run the test.
Please see David Chelimsky's answer to a previous SO question on this topic for a more complete explanation.