#<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x1057fd428>的未定义方法`sign_in'尝试使用 Devise 设置 RSpec 时出错 [英] undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x1057fd428> error while trying to set up RSpec with Devise
问题描述
我有一个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
:
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
Guard 在后台运行并不断抛出这个:
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)
推荐答案
那些测试助手不适用于集成/请求规范.在这些情况下测试 Devise 的推荐方法是访问登录页面,填写表单并提交,然后运行测试.
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.
请参阅 David Chelimsky 对之前的 SO 问题的回答更完整的解释.
Please see David Chelimsky's answer to a previous SO question on this topic for a more complete explanation.
这篇关于#<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x1057fd428>的未定义方法`sign_in'尝试使用 Devise 设置 RSpec 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!