我遇到了一个问题,我的模型上的自定义验证导致所有shoulda验证失败。
实质上:
class User < ActiveRecord::Base
validates_presence_of :name
validate :some_date_validation
private
def some_date_validation
if date_given > birthday
errors.add(:birthday, "Some sort of error message")
end
end
end
Run Code Online (Sandbox Code Playgroud)
然后在规范中:
require 'rails_helper'
RSpec.describe User, type: :model do
describe "shoulda validations" do
it { should validate_presence_of(:name) }
end
end
Run Code Online (Sandbox Code Playgroud)
这将导致我的测试失败,因为其他验证不会通过。为什么是这样?