2

我有以下规格:

  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?!感谢您的澄清。

4

1 回答 1

11

你可以使用subject

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

编辑

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

于 2012-07-12T14:16:04.677 回答