Devise: Rails 5/RSpec/Device - method_missing: undefined method setup

Created on 12 Jul 2016  ·  13Comments  ·  Source: heartcombo/devise

Hello, after upgrade from Rails 5.0.0rc1 to Rails 5.0.0 and try to bundle exec rspec spec/ i get error method_missing': undefined methodsetup'

/Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:732:in `method_missing': undefined method `setup' for RSpec::ExampleGroups::AdminOrdersController:Class (NoMethodError)
    from /Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/devise-4.2.0/lib/devise/test/controller_helpers.rb:28:in `block in <module:ControllerHelpers>'
    from /Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/concern.rb:120:in `class_eval'
    from /Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/concern.rb:120:in `append_features'
    from /Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/devise-4.2.0/lib/devise/test_helpers.rb:9:in `include'
    from /Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/devise-4.2.0/lib/devise/test_helpers.rb:9:in `block in included'
    from /Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/devise-4.2.0/lib/devise/test_helpers.rb:4:in `class_eval'
    from /Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/devise-4.2.0/lib/devise/test_helpers.rb:4:in `included'
    from /Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.5.1/lib/rspec/core/configuration.rb:2063:in `include'
    from /Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.5.1/lib/rspec/core/configuration.rb:2063:in `safe_include'
    from /Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.5.1/lib/rspec/core/configuration.rb:2025:in `block in configure_group_with'
    from /Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.5.1/lib/rspec/core/configuration.rb:2024:in `each'
    from /Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.5.1/lib/rspec/core/configuration.rb:2024:in `configure_group_with'
    from /Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.5.1/lib/rspec/core/configuration.rb:1365:in `configure_group'
    from /Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:429:in `set_it_up'
    from /Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:384:in `subclass'
    from /Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.5.1/lib/rspec/core/example_group.rb:258:in `block in define_example_group_method'
    from /Users/Hristo/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.5.1/lib/rspec/core/dsl.rb:43:in `block in expose_example_group_alias'
    from /Users/Hristo/Work/thc-store/spec/controllers/admin/orders_controller_spec.rb:3:in `<top (required)>'

Gemfile:

source 'https://rubygems.org'

ruby '2.2.4'
gem 'rails', '>= 5.0.0.rc1'
...
group :test do
  gem 'rspec-rails', '~> 3.0'
  gem 'factory_girl_rails', '~> 4.5'
  gem 'shoulda-matchers'
  gem "vcr"
  gem "webmock"
  gem 'timecop'
  gem 'rails-controller-testing'
end

Any help will be appreciated. Thanks.

Needs more info

Most helpful comment

@zephiransas @Blaked84 in those cases you shouldn't include the module inside another directly, but use your ControllerHelpers included hook to include the Devise test helper inside the host test/spec class:

module ControllerHelpers
  extend ActiveSupport::Concern

  included do
    include Devise::Test::ControllerHelpers
  end
end

But it could be something else on your test setup that's breaking with the controller helpers, in that case, please provide a sample app with quick steps to reproduce the error otherwise we will be spitballing here forever. I'm closing this issue as we might have different problems going around here, so if you got a sample app of the issue you are facing please open a new issue with the app's URL.

All 13 comments

See if you have the "setup" script in your repo's "bin" directory.

I have setup script with content:

#!/usr/bin/env ruby
require 'pathname'
require 'fileutils'
include FileUtils

APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)

def system!(*args)
  system(*args) || abort("\n== Command #{args} failed ==")
end

chdir APP_ROOT do
  # This script is a starting point to setup your application.
  # Add necessary setup steps to this file.

  puts '== Installing dependencies =='
  system! 'gem install bundler --conservative'
  system('bundle check') or system!('bundle install')

  # puts "\n== Copying sample files =="
  # unless File.exist?('config/database.yml')
  #   cp 'config/database.yml.sample', 'config/database.yml'
  # end

  puts "\n== Preparing database =="
  system! 'bin/rails db:setup'

  puts "\n== Removing old logs and tempfiles =="
  system! 'bin/rails log:clear tmp:clear'

  puts "\n== Restarting application server =="
  system! 'bin/rails restart'
end

@Blazy7 can you please provide a sample app that reproduces the error? This sound like a possible issue with the controller test setup.

I've also hit this with devise 4.2.0, rails 5.0.0 and rspec-rails 3.5.1. This occurs when trying to run my controller specs.

Rolling back to devise 4.1.0 and changing
config.include Devise::Test::ControllerHelpers, type: :controller
to
config.include Devise::TestHelpers, type: :controller
fixes the issue for me.

This could be an issue with the additional module nesting. setup is defined in the Devise module (line 291), and doesn't seem to be available in the Devise::Test::ControllerHelpers namespace.

devise 4.2.0
Rails 4.2.6
rspec-rails 3.4.2

I write some controller helper for controller test.
in spec/support/controller_helpers.rb like this...

module ControllerHelpers
  include Devise::Test::ControllerHelpers
  (snip)

in rails_helper.rb,

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

when call this line, this issue occured.

Same issue as @zephiransas.

In my mycontroller_controller_spec.rb :
include Devise::TestHelpers

devise 4.2.0
Rails 4.2.3
rspec-rails 3.5.1

If I rollback to devise 4.1.1, this issue disappears.

@zephiransas @Blaked84 in those cases you shouldn't include the module inside another directly, but use your ControllerHelpers included hook to include the Devise test helper inside the host test/spec class:

module ControllerHelpers
  extend ActiveSupport::Concern

  included do
    include Devise::Test::ControllerHelpers
  end
end

But it could be something else on your test setup that's breaking with the controller helpers, in that case, please provide a sample app with quick steps to reproduce the error otherwise we will be spitballing here forever. I'm closing this issue as we might have different problems going around here, so if you got a sample app of the issue you are facing please open a new issue with the app's URL.

@lucasmazza That works well for me. Thx!

Was bitten by this on upgrading devise from 4.1.1 to 4.2.0. Resolved it by merging rspec config from spec_helper.rb to rspec config in rails_helper.rb, for I don't have non-rails tests anyway.

Because it's specified in https://github.com/plataformatec/devise#controller-tests, "Just be sure that this inclusion is made after the require 'rspec/rails' directive."

@lucasmazza I made a quick sample app as you suggested, exhibiting the undefined method 'setup' for RSpec::ExampleGroups:: errors on running rspec after a quick rake db:create && rake db:migrate --> https://github.com/sgoldens/setup-devise-bug

Hope that helps! Would love to get this solved.

@sgoldens your sample app does not require rspec/rails on spec_helper/rails_helper, so it does not load the integrations between rspec and the lifecycle test callbacks that Devise uses (that come from Active Support). Please re-check your test suite setup to ensure that rspec and rails are integrated properly.

This also happens if config.include Devise::Test::ControllerHelpers, type: :controller comes BEFORE `require 'rspec/rails' in your setup.

I've had this issue as well and ended up on this page from Google.

For my application there were 2 locations where config.include Devise::Test::ControllerHelpers, type: :controller was being set, 1 spec/support/devise_config.rb and another in spec/rails_helper.rb . Removing it from spec/support fixed the issue and allowed me to upgrade to 4.2 (and later).

It's unclear to me if it's the duplication causing the issue, or the ordering as @jessethebuilder suggested above.

Was this page helpful?
0 / 5 - 0 ratings