我有一个有效的唯一性测试:
it { should validate_uniqueness_of(:name).case_insensitive }
然而,当我尝试对名称进行类似的测试时,它失败了
it { should validate_presence_of(:name).allow_blank }
我得到这个错误:undefined method
allow_blank'`
最佳答案
根据 this list ,shoulda 中没有可以链接到 validate_presence_of 的方法来支持你想做的事情。 看看the source code of the matcher here .
但是,您可以使用另一个匹配器:
it { should allow_value("", nil).for(:name) }
哪个测试是否可以将空白值应用于您的属性。
完整的测试将是
it { should validate_presence_of(:name) }
it { should allow_value("", nil).for(:name) }
关于ruby-on-rails - rspec - 如何检查 allow_blank 是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11413906/