使用shoulda的NoMethodError validate_presence_of

时间:2011-09-14 08:49:43

标签: ruby-on-rails-3.1 shoulda

好的,我很困惑。我试图在Rails 3.1下使用带有Test :: Unit的shoulda,之前已成功使用Rails 2.3.11。

我的Gemfile中有以下内容:

group :test do
  gem 'shoulda'
end

(我运行bundle install - bundle show shoulda显示c:/Ruby192/lib/ruby/gems/1.9.1/gems/shoulda-2.11.3

我有以下test_helper.rb

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

class ActiveSupport::TestCase
end

以及以下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

但是当我ruby -Itest test\unit\user_test.rb时,我收到以下错误:

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

我未能正确设置什么?

1 个答案:

答案 0 :(得分:7)

解决了它。你需要:

require 'shoulda/rails'
test_helper.rb中的

(不是'require' Shoulda');测试用例需要:

class Usertest < ActiveRecord::TestCase

(不是< ActiveSupport::TestCase)。

我曾单独尝试过这两种方法,但不是一起尝试......