Search code examples
ruby-on-railsvalidationunit-testingminitestcustom-validators

Testing custom validators with Minitest


I have multiple models with email validation. Therefore I've extracted the validation into a custom validator. I dit this by following the tutorial of the Rails Guides.

class EmailValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
      record.errors[attribute] << (options[:message] || "is not an email")
    end
  end
end

So far, so good. But since I've extracted the functionality of email validation into it's own scope I also want to test it separately. I don't want to add the same email format tests to every model.

I found another question which also asked the same but for RSpec. But since I haven't worked with stubs and mocks yet, I don't know how to port the tests to Minitest tests. I haven't found any resources which test custom validators with Minitest.

Does anybody know how to write such tests for custom validators in Minitest (not using specs!)?


Solution

  • What (I think) you are asking for here is testing this validator in isolation. This means that it will be tested once, in an isolated test, which will do exactly what you said:

    I don't want to add the same email format tests to every model.

    The approach I would take here is to create just a test class in a test file, mix-in the ActiveRecord::Validations module and test the class itself.

    # test_file.rb
    require 'test_helper'
    
    class EmailValidatable
      include ActiveModel::Validations
      validates_with EmailValidator
      attr_accessor  :email
    end
    
    class EmailValidatorTest < Minitest::Test
      def test_invalidates_object_for_invalid_email
        obj = EmailValidatable.new
        obj.email = "invalidemail"
        refute obj.valid?
      end
    
      def test_adds_error_for_invalid_email
        obj = EmailValidatable.new
        obj.email = "invalidemail"
        refute_nil obj.errors[:email]
      end
    
      def test_adds_no_errors_for_valid_email
        obj = EmailValidatable.new
        obj.email = "valid@email.com"
        assert_nil obj.errors[:email]
        assert obj.valid?
      end
    end
    

    I haven't tested the code above, but I think that it should give you an idea/direction.

    HTH