7

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!)?

Community
  • 1
  • 1
Tobias
  • 4,523
  • 2
  • 20
  • 40

2 Answers2

6

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

Ilija Eftimov
  • 790
  • 8
  • 16
  • This is exactly what I've search for. I never thought about creating a new class and test it from there. I was always looking for a way on how to testing it directly. But this looks even better! Thanks! – Tobias Dec 02 '15 at 21:52
  • Great, I am happy I helped. Could you pate(bin) the full test of yours? Thanks :) – Ilija Eftimov Dec 02 '15 at 22:53
  • Hi @Ile! I've added my implemented solution as answer. Thank you again! – Tobias Dec 03 '15 at 15:11
4

Here's my implemented solution based on the answer of Ile Eftimov:

require 'test_helper'

class EmailValidatable
    include ActiveModel::Validations
    attr_accessor :email

    validates :email, email: true
end

class EmailValidatorTest < ActiveSupport::TestCase
    invalid_email_addresses = ['invalid email@example.com', 'invalid@ example.com', 'invalid@@example.com', 'invalid', 'invalid@example']

    def obj; @obj ||= EmailValidatable.new; end

    test 'should invalidate email address' do
        invalid_email_addresses.each do |email|
            obj.email = email
            assert_not obj.valid?
        end
    end

    test 'should add error for invalid email' do
        invalid_email_addresses.each do |email|
            obj.email = email
            obj.valid?
            assert_equal I18n.t('errors.messages.not_an_email_address'), obj.errors[:email][0], "no error message for invalid email address \"#{email}\""
        end
    end

    test 'should validate email address' do
        obj.email = FFaker::Internet.email
        assert obj.valid?
    end

    test 'should add no error for valid email' do
        obj.email = FFaker::Internet.email
        assert obj.errors[:email].blank?
    end
end

Really cool solution and is also usable for other tests.

Tobias
  • 4,523
  • 2
  • 20
  • 40