NoMethodError validate_presence_of using shoulda

Ok, I'm puzzled. I am trying to use shoulda with Test :: Unit under Rails 3.1, having previously done this successfully with Rails 2.3.11.

My Gemfile has the following:

group :test do gem 'shoulda' end 

(and I ran bundle install - bundle show shoulda shows c:/Ruby192/lib/ruby/gems/1.9.1/gems/shoulda-2.11.3 )

I have the following test_helper.rb

 ENV["RAILS_ENV"] = "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' require 'shoulda' class ActiveSupport::TestCase end 

and next user_test.rb

 require 'test_helper' class UserTest < ActiveSupport::TestCase should validate_presence_of :email should validate_format_of(:email).with(" user+throwaway@subdom.example.com ").with_message(/valid email address/) should validate_presence_of(:encrypted_password) should validate_confirmation_of :password end 

But when I do ruby -Itest test\unit\user_test.rb , I get the following error:

  test/unit/user_test.rb:4:in `<class:UserTest>': undefined method `validate_presence_of' for UserTest:Class (NoMethodError) 

What I was not able to configure correctly?

+4
source share
1 answer

I decided. You need:

 require 'shoulda/rails' 

in test_helper.rb (not `require 'shoulda'); and the test case should be:

 class Usertest < ActiveRecord::TestCase 

(not < ActiveSupport::TestCase ).

I tried both of them individually, but not together ...

+7
source

All Articles