allegroplanet / allegro-planet

1 stars 1 forks source link

Add StringFormatValidator and Test #10

Closed MarkOates closed 7 years ago

MarkOates commented 7 years ago

Problem

There will be many validations required for various string formats, and as a result, there will be a lot of duplicate code.

Solution

Add a custom StringFormatValidator along with a custom assertion assert_validates_format_rules to use in model tests with those validations.

How it's Used

In use, you might have a test for a model Foo and want to check that certain validations exist on it's :bar property.

class FooTest < ActiveSupport::TestCase
  test 'bar attribute validates with the expected rules' do
    expected_rules = [:starts_with_non_whitespace, :has_only_printable_characters]
    assert_validates_format_rules expected_rules, Foo, :bar
  end
end