Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate_presence_of broken in 3.0.0 #797

Closed
baburdick opened this issue Oct 8, 2015 · 7 comments
Closed

validate_presence_of broken in 3.0.0 #797

baburdick opened this issue Oct 8, 2015 · 7 comments

Comments

@baburdick
Copy link

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
@mcmire
Copy link
Collaborator

mcmire commented Oct 8, 2015

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.

@zekefast
Copy link

@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 ;)

@mcmire
Copy link
Collaborator

mcmire commented Oct 13, 2015

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

@brianjlandau
Copy link

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.

@mcmire
Copy link
Collaborator

mcmire commented Oct 20, 2015

@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.

@brianjlandau
Copy link

@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.

@giedriusr
Copy link

@mcmire thanks. Your info helped!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants