undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x1057fd428> error while trying to set up RSpec with Devise

时间秒杀一切 提交于 2019-12-12 10:47:26

问题


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)


回答1:


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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/7141763/undefined-method-sign-in-for-rspeccoreexamplegroupnested-1nested-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!