我有两个模型:
class Region < ActiveRecord::Base
has_one :acol, :dependent => :nullify
before_destroy :check_acol_presence
private
def check_acol_presence
if acol
errors.add(:base,"activerecord.errors.models.region.delete_with_existing_acol")
return false
end
end
end
class Acol < ActiveRecord::Base
belongs_to :region
end
我想检查 RSpec 测试中的“check_acol_presence”钩子。所以这里是测试代码:
region = FactoryGirl.create(:region)
acol = FactoryGirl.create(:acol, :region => region)
region.reload
region.destroy
lambda { region.reload }.should_not raise(ActiveRecord::RecordNotFound)
在 Rails 控制台中,这个检查工作得很好。但是测试失败了。为什么?