Shoulda/RSpec matchers - conditional validation

前端 未结 1 992
失恋的感觉
失恋的感觉 2021-01-30 08:11

In my code I had the following validation with Shoulda matchers, which works fine:

it { should validate_presence_of(:name) }

In my model, I\'ve

相关标签:
1条回答
  • 2021-01-30 09:01

    It doesn't appear that shoulda_matchers does this, but it's easy enough to write it yourself::

      context "if eligible" do
        before { allow(subject).to receive(:eligible?).and_return(true) }
        it { should validate_presence_of(:name) }
      end
    
      context "if ineligible" do
        before { allow(subject).to receive(:eligible?).and_return(false) }
        it { should_not validate_presence_of(:name) }
      end
    
    0 讨论(0)
提交回复
热议问题