Skip to content

Instantly share code, notes, and snippets.

@delba
Last active December 17, 2015 19:19
Show Gist options
  • Select an option

  • Save delba/5659146 to your computer and use it in GitHub Desktop.

Select an option

Save delba/5659146 to your computer and use it in GitHub Desktop.
Testing validations with I18n and validators_on
require 'test_helper'
class QuestionTest < ActiveSupport::TestCase
include ActionView::Helpers::TranslationHelper
test "title is required" do
question = Question.new.tap(&:valid?)
assert_includes question.errors[:title],
t('errors.messages.blank')
end
test "title is required" do
validators = Question.validators_on(:title)
klass = ActiveRecord::Validations::PresenceValidator
refute_empty validators.grep(klass)
end
delegate :validators_on, to: Question
test "title is required" do
refute_empty validators_on(:title).grep PresenceValidator
end
private
def self.const_missing(const)
return unless const =~ /Validator\z/
ActiveRecord::Validations.const_get(const)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment