为包含ActiveModel :: Validations的类编写测试

时间:2012-04-11 02:14:43

标签: ruby ruby-on-rails-3 rspec2 shoulda

如果你正在使用shoulda-matchers + rspec来测试ActiveRecord类,你可以这样做:

class PersonAsActiveRecord < ActiveRecord::Base
  validates :title, :presence => true
end

describe PersonAsActiveRecord do
  subject { PersonAsActiveRecord.new(:title => 'Lt Wiggles') } 

  it { should validate_presence_of(:title) }
end

但是,如果这似乎不起作用:

class PersonAsJustClass
  include ActiveModel::Validations
  attr_accessor :title
  validates :title, :presence => true
end

describe PersonAsJustClass do
  subject { PersonAsJustClass.new(:title => 'Lt Wiggles') } 

  it { should validate_presence_of(:title) }
end

为什么会这样?或者我做错了什么?

0 个答案:

没有答案