Shoulda-matchers: undefined method `validate_presence_of'

Created on 30 May 2013  路  18Comments  路  Source: thoughtbot/shoulda-matchers

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?

馃洡 Rails 4

Most helpful comment

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

All 18 comments

I'm getting the same thing. I've also tried to require 'shoulda-matchers' in my test_helper.rb file, using the main shoulda gem, as well as other dev branches. Can't get it to work (I'm running rails-4.0.0.rc1 and using ActiveSupport::TestCase). Every shoulda method I try returns "undefined method" like this:

undefined method `validate_presence_of' for PostTest:Class

+1

What made this work for me is from this gist: https://gist.github.com/dougc84/5789552

This is lib/shoulda/matchers/integrations/test_unit.rb. I will be committing a version on github.com/dougc84/shoulda-matchers with this change. I will not be maintaining it, but @thoughtbot, feel free to adapt this to your code. I don't know how you all want this to work, and I don't have time to put together a full-on PR.

The two main problems are:

  1. The namespaces have changed in Rails 4. includes and extends should be on ActiveSupport::TestCase instead of Test::Unit::TestCase.
  2. assert_block is being called... somewhere. Might be in the RC of Rails 4 where something's not right, might be in shoulda-context, I dunno, but it's not in this gem. I wrote a little hack method to give the functionality back using the deprecation warning's recommended approach.

Hope this helps to resolve this issue.

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

Thanks for the input on this you guys and good call on extending ActiveSupport::TestCase instead of Test::Unit::TestCase. I'm not sure why we are currently doing this but I think we should have been extending ActiveSupport::TestCase all along.

Closing this because the dp-rails-four branch already addresses this.

Actually, I'll keep this open until the fix lands in master. The commit that fixed this was here though: 4f109893ae91bcd7ca29bc46ab2c5bb859402e19

This is in master now.

excellent, thank you @mcmire !!!

I was trying to investigate if the commit is yet part of any taged release.

I cannot see that 4f109893ae91bcd7ca29bc46ab2c5bb859402e19 has been merged into master.

What commit in master solves this?

Ah, I figured out. its commit e6486849.
It has not yet been part of a stable release...
Could we please have a stable gem release including this fix as Rails 4 has been out for a while now.

I'll cut a release later today.

Was this ever resolved? Getting this with Rails 4.1.0 & shoulda-matchers 2.6.1 (with RSpec not TestUnit). Not sure if related or not.

Are you using Spring (i.e. bin/rake or bin/rspec) to run your tests?

Nope.

The problem described in this issue was fixed in 2.5.0. I'm not sure why you're getting this error without more information. Is there anything special to your test setup?

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

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

Was this page helpful?
0 / 5 - 0 ratings