我有以下规格:

  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/

10-12 05:16