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!)?
Aucun commentaire:
Enregistrer un commentaire