1

如果你使用 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

为什么会这样?还是我做错了什么?

4

0 回答 0