Testing custom validators with Minitest

Tobias

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

Ilija Eftimov

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

minitest controller testing failure

From Dev

Testing update action in Minitest

From Dev

Minitest: Testing for Infinity

From Dev

minitest controller testing failure

From Dev

CausesValidation not working with custom validators

From Dev

Spring MVC custom validators

From Dev

Django Custom Validators Not Working

From Dev

Proptypes custom validators with Flow

From Dev

Custom validators in WTForms using Flask

From Dev

minitest assert custom assertion fails

From Dev

Unit testing models that use validators with injected dependencies

From Dev

Handle authentication with Capybara / Minitest for integration testing

From Dev

Testing User Model (Devise Authentication) with MiniTest

From Dev

Minitest - testing helper method that uses request params

From Dev

How minitest testing framework works in rails?

From Dev

Handle authentication with Capybara / Minitest for integration testing

From Dev

Minitest - testing helper method that uses request params

From Dev

Abstract Factory for custom Validators - options discarded?

From Dev

(Laravel) How to add errors in custom validators?

From Dev

Creating dependant custom validators for form inputs?

From Dev

Custom Angular Validators- Passing an Argument

From Dev

Create custom validators for dynamic forms angular 2

From Dev

Angular 4 - Using Asynchronous custom validators

From Dev

Generating a minitest :spec test for an existing controller for functional testing

From Dev

Generating a minitest :spec test for an existing controller for functional testing

From Dev

Testing ActiveJob with Minitest doesn't hit Sidekiq queue

From Dev

Spring MockMVC - How to mock custom validators running outside of controllers

From Dev

Do you have to register your custom validators in symfony

From Dev

JSR303 custom validators being called twice

Related Related

  1. 1

    minitest controller testing failure

  2. 2

    Testing update action in Minitest

  3. 3

    Minitest: Testing for Infinity

  4. 4

    minitest controller testing failure

  5. 5

    CausesValidation not working with custom validators

  6. 6

    Spring MVC custom validators

  7. 7

    Django Custom Validators Not Working

  8. 8

    Proptypes custom validators with Flow

  9. 9

    Custom validators in WTForms using Flask

  10. 10

    minitest assert custom assertion fails

  11. 11

    Unit testing models that use validators with injected dependencies

  12. 12

    Handle authentication with Capybara / Minitest for integration testing

  13. 13

    Testing User Model (Devise Authentication) with MiniTest

  14. 14

    Minitest - testing helper method that uses request params

  15. 15

    How minitest testing framework works in rails?

  16. 16

    Handle authentication with Capybara / Minitest for integration testing

  17. 17

    Minitest - testing helper method that uses request params

  18. 18

    Abstract Factory for custom Validators - options discarded?

  19. 19

    (Laravel) How to add errors in custom validators?

  20. 20

    Creating dependant custom validators for form inputs?

  21. 21

    Custom Angular Validators- Passing an Argument

  22. 22

    Create custom validators for dynamic forms angular 2

  23. 23

    Angular 4 - Using Asynchronous custom validators

  24. 24

    Generating a minitest :spec test for an existing controller for functional testing

  25. 25

    Generating a minitest :spec test for an existing controller for functional testing

  26. 26

    Testing ActiveJob with Minitest doesn't hit Sidekiq queue

  27. 27

    Spring MockMVC - How to mock custom validators running outside of controllers

  28. 28

    Do you have to register your custom validators in symfony

  29. 29

    JSR303 custom validators being called twice

HotTag

Archive