Shoulda-matchers: validate_presence_of broken in 3.0.0

Created on 8 Oct 2015  路  7Comments  路  Source: thoughtbot/shoulda-matchers

Downgrading to 2.8.0 fixes this:

     Failure/Error: it { is_expected.to validate_presence_of :drivers_license_state  }
     NoMethodError:
       undefined method `validate_presence_of' for #<RSpec::ExampleGroups::CustomerCoreDriversLicenseMapper::Validation:0x0000010b3fb9f0>
     # ~/.rvm/gems/ruby-2.1.2@customer_core/gems/rspec-expectations-3.3.1/lib/rspec/matchers.rb:966:in `method_missing'
     # ~/.rvm/gems/ruby-2.1.2@customer_core/gems/rspec-core-3.3.2/lib/rspec/core/example_group.rb:671:in `method_missing'

I have the recommended configuration in spec/spec_helper.rb:

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

Most helpful comment

@brianjlandau Requiring shoulda-matchers manually in rails_helper and adding require: false to the Gemfile is no longer needed. The gem now requires a configuration block in rails_helper in order to properly set it up. Please read NEWS.md for more details, as well as information on other backward-incompatible changes.

All 7 comments

In 3.0, matchers are now mixed into specific example groups tagged with :model, :controller, or :routing. If your example group isn't tagged with this or isn't in any of the standard locations, then you'll need to mix a module into your example group, in this case the Shoulda::Matchers::ActiveModel module.

@mcmire it would be good to add that somewhere to documentation. Because if shoulda-matchers is used without rspec-rails, but with activerecord :model tag does not mixed automatically to model tests.

@baburdick ;)

Great, I added a new issue for this above. Closing this one now.

This should be reopened IMO.

I'm running

Ruby: 2.2.3
Rails: 4.2.4
rspec: 3.3.0
rspec-rails: 3.3.3
shoulda-matcher: 3.0.0

Gemfile:

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

gem 'rails', '~> 4.2.4'

# other gems ...

group :test do
  gem 'shoulda-matchers'
  gem 'capybara'
  gem 'simplecov', require: false
end

group :development, :test do
  gem 'rspec-rails'
  gem 'factory_girl_rails'
end

spec/rails_helper.rb:

ENV["RAILS_ENV"] ||= 'test'

require 'simplecov' if ENV['COV']

require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'shoulda/matchers'
require 'capybara/rspec'

# This line has the downside of increasing the boot-up time by
# auto-requiring all files in the support directory.
# Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
  config.infer_spec_type_from_file_location!

  config.include FactoryGirl::Syntax::Methods
  config.include Capybara::DSL
end

suppress_warnings { BCrypt::Engine::DEFAULT_COST = 4 }

Model file (app/models/foo.rb):

class Foo
  include ActiveModel::Model

  attr_accessor :title, :body

  validates :title, presence: true

end

Model spec (spec/models/foo_spec.rb):

require 'rails_helper'

RSpec.describe Foo, type: :model do
  it { should validate_presence_of(:title) }
end

Output from running bundle exec rake spec:

     Failure/Error: it { should validate_presence_of(:title) }
     NoMethodError:
       undefined method `validate_presence_of' for #<RSpec::ExampleGroups::Foo:0x007f99d61d1268>
     # ./spec/models/foo_spec.rb:4:in `block (2 levels) in <top (required)>'

Adding require: false to the Gemfile did not help. I am not using TestUnit or any other testing framework.

Please advise.

@brianjlandau Requiring shoulda-matchers manually in rails_helper and adding require: false to the Gemfile is no longer needed. The gem now requires a configuration block in rails_helper in order to properly set it up. Please read NEWS.md for more details, as well as information on other backward-incompatible changes.

@mcmire Got it, thanks! Surprised auto-configuring has caused so much trouble, I always found it worked perfectly. But thanks for pointing me in the way of documentation for this.

@mcmire thanks. Your info helped!

Was this page helpful?
0 / 5 - 0 ratings