ale*_*333 61 ruby rspec ruby-on-rails shoulda
在我的代码中,我使用了Shoulda匹配器进行了以下验证,效果很好:
it { should validate_presence_of(:name) }
Run Code Online (Sandbox Code Playgroud)
在我的模型中,我已将条件添加到我的验证中:
validates_presence_of :name, :if => eligible?
Run Code Online (Sandbox Code Playgroud)
是否有可能在验证中反映出来?
我已经尝试过查看shoulda匹配器的文档,但无法找到解决方案.
非常感谢!
zet*_*tic 126
似乎shoulda_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
Run Code Online (Sandbox Code Playgroud)