shoulda-matchers: undefined method `validate_presence_of'

I’m getting an error when trying to use shoulda-matchers in a Rails 4 project:

test/models/registration_test.rb:5:in `<class:RegistrationTest>': undefined method `validate_presence_of' for RegistrationTest:Class (NoMethodError) from test/models/registration_test.rb:3:in `<main>'

Gemfile:

group :test do
  gem 'capybara'
  gem 'shoulda-matchers'
  gem 'database_cleaner'
  gem 'factory_girl_rails'
end

test_helper.rb:

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

class ActiveSupport::TestCase
  ActiveRecord::Migration.check_pending!
end

registration_test.rb:

require 'test_helper'

class RegistrationTest < ActiveSupport::TestCase

  should validate_presence_of :first_name

end

What am I missing here?

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 18 (1 by maintainers)

Most upvoted comments

The quick & dirty hack version of the above:

diff --git a/test/test_helper.rb b/test/test_helper.rb
index bc7e05d..65a62af 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -5,11 +5,17 @@ require 'rails/test_help'
 class ActiveSupport::TestCase
   ActiveRecord::Migration.check_pending!

+
+  include Shoulda::Matchers::ActiveRecord
+  extend Shoulda::Matchers::ActiveRecord
+  include Shoulda::Matchers::ActiveModel
+  extend Shoulda::Matchers::ActiveModel
+
   # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
   #

@onetakeone Not only is this issue closed (and was closed 4 years ago), but the last comment was over 3 years ago. Don’t comment on dead threads.

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