来自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
为什么即使没有指定姓氏,上述测试也会通过?
答案 0 :(得分:1)
last_name
。使用Factory.build
创建配置文件,该:profile
返回Factory.define :profile do |p|
p.first_name "Robert"
p.last_name "Grimm"
end
的预定义模拟,
{{1}}
答案 1 :(得分:0)
我怀疑Factory.build(:profile, ...)
调用会创建一个默认first_name
和last_name
设置的配置文件模型,除非另有说明(在此示例中为:first_name => nil
)。
然而,这只是一个有根据的猜测,我从上面的代码和我看到的here推断出来。