Undefined method `sign_in 'for # <RSpec :: Core :: ExampleGroup :: Nested_1 :: Nested_1: 0x1057fd428> error while trying to configure RSpec using Devise
I have 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 runs 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 spent the last couple of hours trying to tune various settings and different syntaxes, but nothing changes. Any ideas?
(edited to reflect the newer bug)
These test assistants will not work for integration / query specifications. The recommended way to test Devise in these situations is to visit the login page, fill out the form and submit it, and then run the test.
Please see David Chelimsky's answer to the previous SO question on this topic for a more complete explanation.
An ideal solution would be to create a file in spec / support / devise.rb and include development test assistants in the Rspec configuration through the following code:
Rspec.configure do |config| config.include Devise::TestHelpers, :type => :controller end The type parameter includes helpers only in the specifications of your controller in order to avoid future problems that may arise as a result of calling it when testing models or views. It's not obligatory.
The reason we decided to add a discrete file to include helpers, as opposed to including them in the specifications, as mentioned above, in the sense that if the specifications are restored, the specification will be overwritten.