为什么 Diaspora 应用程序中的测试没有失败?
来自 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
为什么即使未指定姓氏,上述测试也会通过?
From 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
But in http://github.com/diaspora/diaspora/blob/master/app/models/profile.rb is validated the presense of both, the first and last name like so validates_presence_of :first_name, :last_name
Why does the above test pass even though a last name isn't specified?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
last_name
实际上是指定的。配置文件是使用Factory.build
创建的,它返回:profile
的预定义模拟,即last_name
is actually specified. The profile is create using theFactory.build
, which returns the predefined mock of:profile
, which is我怀疑
Factory.build(:profile, ...)
调用会创建一个具有默认first_name
和last_name
设置的配置文件模型,除非指定否则(通过本例中的:first_name => nil
)。然而,这只是我从上面的代码和我所看到的内容中推断出的有根据的猜测 这里。
I suspect the
Factory.build(:profile, ...)
call creates a profile model with a defaultfirst_name
andlast_name
set, unless specified otherwise (by the:first_name => nil
in this example).However that's just an educated guess which I am inferring from the code above and what I see here.