来自http://github.com/diaspora/diaspora/blob/master/spec/models/profile_spec.rb
describe Profile do
before do
@person = Factory.build(:person)
end
describe 'requirements' do
it "should include a first name" do
@person.profile = Factory.build(:profile,:first_name => nil)
@person.profile.valid?.should be false
@person.profile.first_name = "Bob"
@person.profile.valid?.should be true
end
end
end但在http://github.com/diaspora/diaspora/blob/master/app/models/profile.rb中,validates_presence_of :first_name, :last_name这样的名字和姓氏都会被验证
为什么即使没有指定姓氏,上面的测试也能通过?
发布于 2010-09-24 23:19:37
我怀疑Factory.build(:profile, ...)调用创建了一个带有默认first_name和last_name集的概要文件模型,除非另外指定(本例中由:first_name => nil指定)。
然而,这只是一个有根据的猜测,我是从上面的代码和我看到的here中推断出来的。
发布于 2010-09-24 23:20:30
实际指定的是last_name。配置文件是使用Factory.build创建的,它返回预定义的:profile模拟,即
Factory.define :profile do |p|
p.first_name "Robert"
p.last_name "Grimm"
endhttps://stackoverflow.com/questions/3788450
复制相似问题