4

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

I have the following in my Gemfile:

group :test do
  gem 'shoulda'
end

(and I've run 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 the following 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 have I failed to set up properly?

Chowlett
  • 45,935
  • 20
  • 116
  • 150

1 Answers1

7

Solved it. You need:

require 'shoulda/rails'

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

class Usertest < ActiveRecord::TestCase

(not < ActiveSupport::TestCase).

I'd tried both of those individually, but not together...

Chowlett
  • 45,935
  • 20
  • 116
  • 150