Module Shoulda::ActiveRecord::Assertions
In: lib/shoulda/active_record/assertions.rb
ValidationMatcher ValidatePresenceOfMatcher ValidateAcceptanceOfMatcher EnsureLengthOfMatcher ValidateFormatOfMatcher EnsureInclusionOfMatcher ValidateUniquenessOfMatcher ValidateNumericalityOfMatcher Context SetSessionMatcher RespondWithContentTypeMatcher SetTheFlashMatcher RenderWithLayout RespondWithMatcher AssignToMatcher FilterParamMatcher RouteMatcher HaveNamedScopeMatcher HaveDbIndexMatcher HaveDbColumnMatcher AllowMassAssignmentOfMatcher AllowValueMatcher AssociationMatcher HaveReadonlyAttributeMatcher lib/shoulda/context.rb lib/shoulda/action_controller/matchers/set_session_matcher.rb lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb lib/shoulda/action_controller/matchers/respond_with_matcher.rb lib/shoulda/action_controller/matchers/assign_to_matcher.rb lib/shoulda/action_controller/matchers/filter_param_matcher.rb lib/shoulda/action_controller/matchers/route_matcher.rb Matchers Macros ActionController lib/shoulda/active_record/matchers/validation_matcher.rb lib/shoulda/active_record/matchers/have_named_scope_matcher.rb lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb lib/shoulda/active_record/matchers/have_db_index_matcher.rb lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb lib/shoulda/active_record/matchers/validate_format_of_matcher.rb lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb lib/shoulda/active_record/matchers/have_db_column_matcher.rb lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb lib/shoulda/active_record/matchers/allow_value_matcher.rb lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb lib/shoulda/active_record/matchers/association_matcher.rb lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb Matchers Helpers Assertions Macros ActiveRecord ClassMethods Macros ActionView Private Helpers Assertions ActionMailer InstanceMethods Assertions Macros Shoulda dot/m_47_0.png

Methods

Public Instance methods

Asserts that an Active Record model invalidates the passed value by making sure the error_message_to_expect is contained within the list of errors for that attribute.

  assert_bad_value(User.new, :email, "invalid")
  assert_bad_value(User.new, :ssn, "123", /length/)

If a class is passed as the first argument, a new object will be instantiated before the assertion. If an instance variable exists with the same name as the class (underscored), that object will be used instead.

  assert_bad_value(User, :email, "invalid")

  product = Product.new(:tangible => true)
  assert_bad_value(product, :price, "0")

[Source]

    # File lib/shoulda/active_record/assertions.rb, line 59
59:       def assert_bad_value(object_or_klass, attribute, value,
60:                            error_message_to_expect = nil)
61:         object = get_instance_of(object_or_klass)
62:         matcher = allow_value(value).
63:                     for(attribute).
64:                     with_message(error_message_to_expect)
65:         assert_rejects(matcher, object)
66:       end

Asserts that an Active Record model validates with the passed value by making sure the error_message_to_avoid is not contained within the list of errors for that attribute.

  assert_good_value(User.new, :email, "user@example.com")
  assert_good_value(User.new, :ssn, "123456789", /length/)

If a class is passed as the first argument, a new object will be instantiated before the assertion. If an instance variable exists with the same name as the class (underscored), that object will be used instead.

  assert_good_value(User, :email, "user@example.com")

  product = Product.new(:tangible => false)
  assert_good_value(product, :price, "0")

[Source]

    # File lib/shoulda/active_record/assertions.rb, line 35
35:       def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = nil)
36:         object = get_instance_of(object_or_klass)
37:         matcher = allow_value(value).
38:                     for(attribute).
39:                     with_message(error_message_to_avoid)
40:         assert_accepts(matcher, object)
41:       end

Asserts that the given object can be saved

 assert_save User.new(params)

[Source]

    # File lib/shoulda/active_record/assertions.rb, line 7
 7:       def assert_save(obj)
 8:         assert obj.save, "Errors: #{pretty_error_messages obj}"
 9:         obj.reload
10:       end

Asserts that the given object is valid

 assert_valid User.new(params)

[Source]

    # File lib/shoulda/active_record/assertions.rb, line 15
15:       def assert_valid(obj)
16:         assert obj.valid?, "Errors: #{pretty_error_messages obj}"
17:       end

[Validate]