shoulda: undefined method `validate_presence_of' in shoulda 3.0.1, Rails 3.2 and RSpec-Rails 2.8.1

Seem to be unable to fix an error saying:

Failure/Error: it { should validate_presence_of(:username) }
 NoMethodError:
   undefined method `validate_presence_of' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007fee4f3aa9b8>

Also using ruby-1.9.2

About this issue

  • Original URL
  • State: closed
  • Created 12 years ago
  • Comments: 78 (1 by maintainers)

Commits related to this issue

Most upvoted comments

It works for me:

# spec/spec_helper.rb
Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end
# spec/models/post_spec.rb
RSpec.describe Post, type: :model do
   it { should validate_presence_of(:title) }
end

Thanks @zekefast

rails 4.2.4 shoulda matchers 3.0.1 all fixes after adding to spec\spec_helper.rb

require "bundler/setup"
::Bundler.require(:default, :test)
require "shoulda/matchers"
Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

It seems that reverting shoulda-matcher back to version 2.5.0 fix this.

After everything, it still wouldn’t work in Rails 4.2+. I had to add type: :model.

RSpec.describe Foo, type: :model do

Same here:

  • Ruby: 2.2.3,
  • RSpec: 3.3.0
  • ActiveRecord: 4.2.4
  • ActiveModel: 4.2.4

it works for 2.8.0 and does not work with 3.0.0.

Configuration for 2.8.0

# Gemfile
group :test do
  gem "shoulda-matchers", "< 3.0.0",
    require: false
end
# spec/spec_helper.rb

# no require of rails or active record, suppose this loads them
require "bundler/setup"
::Bundler.require(:default, :test)

require "shoulda-matchers"

Configuration for 3.0.0

# Gemfile
group :test do
  gem "shoulda-matchers", ">= 3.0.0",
    require: false
end
# spec/spec_helper.rb

# no require of rails or active record, suppose this loads them
require "bundler/setup"
::Bundler.require(:default, :test)

require "shoulda/matchers"
::Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    # Choose a test framework:
    with.test_framework :rspec
    #with.test_framework :minitest
    #with.test_framework :minitest_4
    #with.test_framework :test_unit

    # Choose one or more libraries:
    with.library :active_record
    with.library :active_model
    #with.library :action_controller
    # Or, choose the following (which implies all of the above):
    #with.library :rails
  end
end

RSpec.configure do |config|
  # ...
end

After some hours of research here’s a solution that worked for me.

require 'shoulda/matchers'

should go after

require 'rspec/rails'

in rails_helper.rb(not at the top of spec_helper.rb).

I’m confused why this issue has been going for so long and through so many different versions of the gem and Rails. I see people doing some weird require order hacking in order to get it to work. I used @bsodmike’s trick (RSpec.describe Foo, type: :model do) but effectively in Rails 4.2.6 with the latest version (3.1.1), this gem is broken by default.

@stefanhendriks - the shoulda-matchers method is validate_presence_of, not validates_presence_of. And I’m pretty sure it needs to be within an it block, i.e.:

describe Developer do
  it { should validate_presence_of(:name) }
end

Setting for rails 4.2.5 and ruby 2.2.4

Gemfile

group :test do
  gem 'shoulda-matchers', '~> 3.0'
end

spec/shoulda_matchers_helper.rb

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

spec/rails_helper.rb

require 'spec_helper'
require 'shoulda_matchers_helper'
require 'rspec/rails'

Using RoR 4.2.6 this works for me.

group :test do
  gem 'test-unit', '~> 3.2'
  gem 'shoulda', '~> 3.5'
end

@wbigal your fix worked for me. Can we add that to the documentation?

Same problem here with Rails 4.2, shoulda-matchers 2.8.0, rspec 3.2.

  3) User 
     Failure/Error: it { is_expected.to validate_presence_of :email }
     NoMethodError:
       undefined method `validate_presence_of' for #<RSpec::ExampleGroups::User:0x007fc072785e30>
     # ./spec/models/user_spec.rb:5:in `block (2 levels) in <top (required)>'

None of the following solutions have worked for me:

  • downgrade to shoulda-matchers 2.5
  • using require ‘shoulda/matchers’ or require ‘shoulda-matchers’
  • move the gem around in test group

Here’s my Gemfile:

source 'https://rubygems.org'
gem 'rails', '4.2.0'
gem 'pg'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'foundation-rails'
gem 'haml'
gem 'haml-rails' 
gem 'devise'
group :development, :test do
  gem 'byebug'
  gem 'web-console', '~> 2.0'
  gem 'spring'

  gem 'rspec-rails', '~> 3.0'
  gem 'capybara'
  gem 'factory_girl_rails'
end

gem 'shoulda-matchers', require: false, group: :test

and my spec_helper.rb:

require 'capybara/rspec'
require 'factory_girl_rails'
require 'shoulda/matchers'

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end
end

Reverse shoulda-matcher to 2.5.0 fix the issue for me also, thanks @hieuk09 .

Check https://github.com/thoughtbot/shoulda-matchers for configuration block to place in rails_helper.rb

@karlingen, @wbigal The issue was fixed for me, by adding type: :model tag to tests. More details here.

I was able to resolve this in another project by adding gem 'shoulda-matchers', require: false to the Gemfile, and then inside of spec_helper adding require 'shoulda/matchers'.