我有以下规格:
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
块中,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
编辑
我刚刚发现了
its
[link]这是测试subject
属性或测试发送给它的消息的好方法关于ruby-on-rails - RSpec/应该:“should be_valid”失败,“contact.should be_valid”通过,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11453751/