Module: EvelpidonTestHelpers::ActiveModel::ModelValidations::Assertions

Included in:
ActiveSupport::TestCase
Defined in:
lib/evelpidon_test_helpers/active_model/model_validations.rb

Instance Method Summary collapse

Instance Method Details

#assert_invalid(object, attributes_with_errors = {}) ⇒ Object

Asserts that the given ActiveModel is “invalid”. The attributes_with_errors options should a hash of attributes to be specifically examined for having errors. For example : => 1, :username => 2 (etc).



18
19
20
21
22
23
24
25
26
27
# File 'lib/evelpidon_test_helpers/active_model/model_validations.rb', line 18

def assert_invalid(object, attributes_with_errors = {})
  assert object.invalid?, "Expected #{object} to be invalid, but was actually valid"

  attributes_with_errors.each do |attribute, expected_number_of_errors|
    actual_errors_on_attribute = object.errors[attribute].length
    error_message = "Expected #{expected_number_of_errors} errors on #{attribute}, but were actually #{actual_errors_on_attribute} : \n"
    error_message << "#{object.errors[attribute].join("\n")}"
    assert_equal expected_number_of_errors, actual_errors_on_attribute, error_message
  end
end

#assert_valid(object, additional_message = nil) ⇒ Object

Asserts that the given ActiveModel is “valid”. If not, the error message is the full error messages.



7
8
9
10
11
12
13
# File 'lib/evelpidon_test_helpers/active_model/model_validations.rb', line 7

def assert_valid(object, additional_message = nil)
  is_valid = object.valid?
  error_message = additional_message ?
      "#{additional_message}\n\nErrors:\n\n#{object.errors.full_messages.join("\n")}\n\n" :
      "Errors:\n#{object.errors.full_messages.join("\n")}\n\n"
  assert is_valid, error_message
end

#assert_valid_model_attributes(attributes) ⇒ Object

Asserts that the given attributes result to a valid ActiveModel instance.

This helper chooses the model name based on the current test name, so for example when the test that is running is named FooTest it will try to instantiate a new Foo model, update the attributes (by-passing mass assignment protection) and then call assert_valid on it.



35
36
37
38
39
40
41
42
43
44
# File 'lib/evelpidon_test_helpers/active_model/model_validations.rb', line 35

def assert_valid_model_attributes(attributes)
  model = self.class.to_s.gsub("Controller", "").gsub("Test", "").constantize
  instance = model.new

  attributes.each do |attribute_name, attribute_value|
    instance.send("#{attribute_name}=", attribute_value)
  end

  assert_valid instance, instance.inspect
end