未定义方法“sign_in”对于#尝试使用 Devise 设置 RSpec 时出错

发布于 2024-11-30 17:10:24 字数 1635 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深者入戏 2024-12-07 17:10:24

理想的解决方案是在 spec/support/devise.rb 创建一个文件,并通过以下代码在 Rspec 配置中包含设计测试助手:

Rspec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

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:

Rspec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

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.

妄想挽回 2024-12-07 17:10:24

由于某种原因,这对我来说也不起作用,所以我只是手动将此助手包含在我的规范中,如下所示:

describe CartItemsController do
  include Devise::TestHelpers

  # ...
end

For some reason this also doesn't work for me so I just manually include this helper in my specs like this:

describe CartItemsController do
  include Devise::TestHelpers

  # ...
end
赴月观长安 2024-12-07 17:10:24

这些测试助手不适用于集成/请求规范。在这些情况下测试 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文