1

我在 rspec 2 上进行了测试,因此如果 partner_id 存在,则 Agency_id 不应该:

describe "tests on deals models validations for AGENCY_ID" do   
  context "if partner_id present" do 
    before { subject.stub(:partner_id) { 2 } } 
    it { should validate_absence_of(:agency_id) }
  end

如何使用 rspec 3 做到这一点?

我试过了:

  it "should reject model if agency_id present AND partner_id is also already present" do
    allow(subject).to receive(:partner_id) { 2 }
    expect(Customer.new).to have(1).errors_on(:agency_id) 
  end
end

但它不起作用,这是一种废话。

4

1 回答 1

1

see here:

context "if partner_id present" do 
  before { allow(subject).to receive(:partner_id) { 2 } } 
  it { is_expected.to validate_absence_of(:agency_id) }
end

Also, in the new syntax you may still use the old one-liner syntax:

should was designed back when rspec-expectations only had a should-based syntax. However, it continues to be available and work even if the :should syntax is disabled (since that merely removes Object#should but this is RSpec::Core::ExampleGroup#should).

So this will also work:

context "if partner_id present" do 
  before { allow(subject).to receive(:partner_id) { 2 } } 
  it { should validate_absence_of(:agency_id) }
end
于 2014-06-07T15:40:05.330 回答