Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 702
Tobias Avatar asked Nov 28 '15 21:11

Tobias


1 Answers

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 = "[email protected]"
    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

like image 156
Ilija Eftimov Avatar answered Oct 12 '22 22:10

Ilija Eftimov