为什么 Diaspora 应用程序中的测试没有失败?

发布于 2024-09-25 10:16:46 字数 870 浏览 3 评论 0原文

来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清晨说晚安 2024-10-02 10:16:46

last_name 实际上是指定的。配置文件是使用 Factory.build 创建的,它返回 :profile 的预定义模拟,即

Factory.define :profile do |p|
  p.first_name "Robert"
  p.last_name "Grimm"
end

last_name is actually specified. The profile is create using the Factory.build, which returns the predefined mock of :profile, which is

Factory.define :profile do |p|
  p.first_name "Robert"
  p.last_name "Grimm"
end
始终不够 2024-10-02 10:16:46

我怀疑 Factory.build(:profile, ...) 调用会创建一个具有默认 first_namelast_name 设置的配置文件模型,除非指定否则(通过本例中的 :first_name => nil)。

然而,这只是我从上面的代码和我所看到的内容中推断出的有根据的猜测 这里

I suspect the Factory.build(:profile, ...) call creates a profile model with a default first_name and last_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文