gpt4 book ai didi

ruby-on-rails - RSpec/Shoulda : "should be_valid" fails, "contact.should be_valid"通过

转载 作者:数据小太阳 更新时间:2023-10-29 08:16:36 25 4
gpt4 key购买 nike

我有以下规范:

  context "no firstname present" do
let(:contact) { build :contact, firstname: 'Mickey', lastname: '', companyname: '' }
it "should be valid" do
should be_valid # Does not work
contact.should be_valid # Works
end
end

为什么 should be_valid 失败,但 contact.should be_valid 通过?在 it block 中,should be_valid 应该访问 contact 吗?!感谢您的澄清。

最佳答案

你可以使用subject

context "no firstname present" do
let(:contact) { build :contact, firstname: 'Mickey', lastname: '', companyname: '' }
subject { contact }
it { should be_valid }
end

编辑

我刚刚发现它的 [link]这是测试 subject 的属性或测试发送给它的消息的好方法

关于ruby-on-rails - RSpec/Shoulda : "should be_valid" fails, "contact.should be_valid"通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11453751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com