尝试为我的自定义验证实现一些测试,我遇到了 this example .
我的验证器看起来像这样
class FutureValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if value == nil
record.errors[attribute] << " can't be nil"
elsif value < Time.now
record.errors[attribute] << (options[:message] || "can't be in the past!")
end
end
end
我已经使用上面提到的示例作为指导来做到这一点。
require 'test_helper'
class FutureValidatorable
include ActiveModel::Validations
# validates_with FutureValidator
attr_accessor :future
validates :future, future: true
end
class FutureValidator < ActiveSupport::TestCase
future_value = [nil]
def obj; @obj ||= FutureValidatorable.new; end
test 'future validator returns proper error code when nil is passed to it' do
future_value.each do |value|
@obj.value = value
assert_not obj.valid?
end
end
在此阶段,我收到的错误消息如下。
rake aborted! TypeError: superclass mismatch for class FutureValidator
我不知道如何从这里继续。
最佳答案
看起来您的模型和 TestCase 具有相同的类名。更改测试的名称应该可以解决问题。
关于ruby-on-rails - 为自定义验证器创建测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36507829/