如果你正在使用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
为什么会这样?或者我做错了什么?