在我的代码中,我对Shoulda matchers进行了以下验证,效果很好:
it { should validate_presence_of(:name) }
在我的模型中,我在验证中添加了以下条件:
validates_presence_of :name, :if => eligible?
有可能在验证中反映出来吗?
我试着寻找documentation个shoulda matchers,但没有找到解决方案.
非常感谢!
在我的代码中,我对Shoulda matchers进行了以下验证,效果很好:
it { should validate_presence_of(:name) }
在我的模型中,我在验证中添加了以下条件:
validates_presence_of :name, :if => eligible?
有可能在验证中反映出来吗?
我试着寻找documentation个shoulda matchers,但没有找到解决方案.
非常感谢!
似乎不应该让matchers这么做,但自己写就足够容易了:
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