Shoulda-matchers: undefined method `alias_method_chain' for RSpec::Matchers::BuiltIn::Have:Class (NoMethodError)

Created on 3 Jul 2017  路  7Comments  路  Source: thoughtbot/shoulda-matchers

Hi, i'm a beginner in ruby and rails and i study rails with rails tutorial. I have a problem with shoulda-matchers i think. i tried to find answer, but i can't.

when i use:

bundle exec rspec spec/requests/static_pages_spec.rb

i get this error:

/home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-rails-2.13.1/lib/rspec/rails/matchers/have_extension.rb:27:in `block in <module:HaveExtensions>': undefined method `alias_method_chain' for RSpec::Matchers::BuiltIn::Have:Class (NoMethodError)
Did you mean?  alias_method
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.2/lib/active_support/concern.rb:120:in `class_eval'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.2/lib/active_support/concern.rb:120:in `append_features'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-rails-2.13.1/lib/rspec/rails/matchers/have_extension.rb:34:in `include'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-rails-2.13.1/lib/rspec/rails/matchers/have_extension.rb:34:in `block in <top (required)>'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-rails-2.13.1/lib/rspec/rails/matchers/have_extension.rb:33:in `class_eval'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-rails-2.13.1/lib/rspec/rails/matchers/have_extension.rb:33:in `<top (required)>'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `require'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `block in require'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:258:in `load_dependency'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `require'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-rails-2.13.1/lib/rspec/rails/matchers.rb:25:in `<top (required)>'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `require'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `block in require'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:258:in `load_dependency'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `require'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-rails-2.13.1/lib/rspec/rails.rb:12:in `<top (required)>'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `require'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `block in require'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:258:in `load_dependency'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:292:in `require'
    from /home/holywalley/rails_projects/tddtest/spec/spec_helper.rb:4:in `<top (required)>'
    from /home/holywalley/rails_projects/tddtest/spec/requests/static_pages_spec.rb:1:in `require'
    from /home/holywalley/rails_projects/tddtest/spec/requests/static_pages_spec.rb:1:in `<top (required)>'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
    from /home/holywalley/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'

my Gemfile looks:

source 'https://rubygems.org'
ruby '2.3.1'
#ruby-gemset=railstutorial_rails_4_0

gem 'rails', '5.1.2'
gem 'railties', '5.1.2'
gem 'rake', '< 11.0'
gem 'activesupport', '5.1.2'
gem 'test-unit', '3.2.5'
gem 'i18n'

group :development, :test do
  gem 'sqlite3', '1.3.13'
  gem 'rspec-rails', '2.13.1'
  gem 'listen'
  gem 'shoulda-matchers', require: false
  #gem 'shoulda-matchers', git: 'https://github.com/thoughtbot/shoulda-matchers.git', branch: 'rails-5'
end

group :test do
  gem 'selenium-webdriver', '2.35.1'
  gem 'capybara', '2.1.0'
end

gem 'sass-rails', '5.0.6'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.2.2'
gem 'jquery-rails', '4.3.1'
gem 'turbolinks', '5.0.1'
gem 'jbuilder', '2.7.0'
gem 'sprockets-rails', '3.2.0'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

group :production do
  gem 'pg', '0.21.0'
  gem 'rails_12factor', '0.0.2'
  gem 'sprockets', '3.7.1'
end

static_pages_spec.rb:

require 'spec_helper'

describe "Home page" do

  it "should have the content 'Sample App'" do
    visit '/static_pages/home'
    expect(page).to have_content('Sample App')
  end
end

if i remove require 'spec_helper' i get this:

Failures:

  1) Home page should have the content 'Sample App'
     Failure/Error: visit '/static_pages/home'
     NoMethodError:
       undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x0055bf376dec40>
     # ./spec/requests/static_pages_spec.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.00041 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:4 # Home page should have the content 'Sample App'

sorry if i wrote something bad

Most helpful comment

Hi @HolyWalley,

This doesn't appear to be a shoulda-matchers issue, but I'll give you an answer anyway.

It looks like you're attempting to use Capybara in a request spec. Typically request specs are used to hit API endpoints that you may have created in your app. They are very similar to controller specs. If you want to use Capybara, I would suggest using a feature spec instead. This is as simple as moving your file from spec/requests to spec/features.

Additionally, it also looks like you're using rspec-rails 2.x. This is an old version of the gem which is no longer maintained. It looks like the latest version is 3.6.0, so I would try upgrading and see if that helps. Please note that you will have to require "rails_helper" instead of require "spec_helper" and you will have to configure shoulda-matchers in your rails_helper as you see in the README.

Post back if you continue to have problems.

All 7 comments

Hi @HolyWalley,

This doesn't appear to be a shoulda-matchers issue, but I'll give you an answer anyway.

It looks like you're attempting to use Capybara in a request spec. Typically request specs are used to hit API endpoints that you may have created in your app. They are very similar to controller specs. If you want to use Capybara, I would suggest using a feature spec instead. This is as simple as moving your file from spec/requests to spec/features.

Additionally, it also looks like you're using rspec-rails 2.x. This is an old version of the gem which is no longer maintained. It looks like the latest version is 3.6.0, so I would try upgrading and see if that helps. Please note that you will have to require "rails_helper" instead of require "spec_helper" and you will have to configure shoulda-matchers in your rails_helper as you see in the README.

Post back if you continue to have problems.

Thank you so much. I did everything you advised and I succeeded. How can i thank you?

@HolyWalley Don't worry about it! I'm just happy that I was able to help you :)

I have same problem!I folloowing instructions in your README but don't fix this error

/home/aleksey/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/rspec-rails-2.13.1/lib/rspec/rails/matchers/have_extension.rb:27:in `block in <module:HaveExtensions>': undefined method `alias_method_chain' for RSpec::Matchers::BuiltIn::Have:Class
Did you mean?  alias_method (NoMethodError)

My Gemfile look like this:

source 'https://rubygems.org'
ruby '2.4.0'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end

gem 'rails', '~> 5.1.3'
gem 'puma', '~> 3.7'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
gem 'test-unit', '3.2.5'

group :development, :test do
  gem 'rspec-rails', '2.13.1'
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'
end

group :development do
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

group :production do
  gem 'pg', '0.21.0'
  gem 'rails_12factor'
end

group :test do
  gem 'sqlite3', '1.3.13'
  gem 'shoulda', '~> 3.5.0'
  gem 'shoulda-matchers', '2.8.0'
end

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

And rails_helper look like this:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

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

ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
  config.order = "random"
  config.include Capybara::DSL
end

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

what should i do???

@Alexxeey I'm not exactly sure why you're getting that error, but try using RSpec 3 and shoulda-matchers 3.

Thank you @mcmire i already fix this bug.
My rspec tests does not work but when i use feature spec sintax from source https://relishapp.com/rspec/rspec-rails/docs/feature-specs/feature-spec
#and add require 'capybara/rails' on static_pages_spec.rb file i solve this problem

also i add gem gem 'shoulda-matchers' , gem 'capybara' in :test group and gem 'factory_girl_rails' in :development, :test group

@Alexxeey Great, glad I could help!

Was this page helpful?
0 / 5 - 0 ratings