2

Trying to implement a couple tests for my custom validations and I came across this example.

My validator looks like this

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

And I've gotten as far as this using the above mentioned example as a guideline.

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

At this stage I'm getting the error message I am getting is as follows.

rake aborted! TypeError: superclass mismatch for class FutureValidator

I'm not sure how to proceed from here.

Community
  • 1
  • 1
CheeseFry
  • 1,299
  • 1
  • 21
  • 38

1 Answers1

3

Looks like your model and TestCase have the same class name. Changing the name of your test should fix the issue.

Anthony E
  • 11,072
  • 2
  • 24
  • 44
  • Good catch. I just noticed that 5 seconds before you posted this answer. – CheeseFry Apr 08 '16 at 19:35
  • Any suggestions on how to get a workable test on this one? `NoMethodError: undefined method `value=' for nil:NilClass` is my current error message on the test. – CheeseFry Apr 08 '16 at 19:36
  • looks like your `obj` method isn't being called. Either use `obj` instead of `@obj` in your Test Case, or declare it within a `def setup` method of the test. – Anthony E Apr 08 '16 at 19:47