好吧,我很困惑。我正在尝试在Rails 3.1下将shoulda与Test::Unit一起使用,之前在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)我没有正确设置的是什么?
发布于 2011-09-15 16:45:00
解决了它。您需要:
require 'shoulda/rails'在test_helper.rb中(不是‘`require 'shoulda');测试用例需要是:
class Usertest < ActiveRecord::TestCase(不是< ActiveSupport::TestCase)。
我单独试过这两个,但没有一起试过...
https://stackoverflow.com/questions/7413625
复制相似问题