Module Shoulda::Assertions
In: lib/shoulda/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 the given matcher returns true when target is passed to matches?

[Source]

    # File lib/shoulda/assertions.rb, line 48
48:     def assert_accepts(matcher, target, options = {})
49:       if matcher.matches?(target)
50:         assert_block { true }
51:         if options[:message]
52:           assert_match options[:message], matcher.negative_failure_message
53:         end
54:       else
55:         assert_block(matcher.failure_message) { false }
56:       end
57:     end

Asserts that the given collection contains item x. If x is a regular expression, ensure that at least one element from the collection matches x. extra_msg is appended to the error message if the assertion fails.

  assert_contains(['a', '1'], /\d/) => passes
  assert_contains(['a', '1'], 'a') => passes
  assert_contains(['a', '1'], /not there/) => fails

[Source]

    # File lib/shoulda/assertions.rb, line 23
23:     def assert_contains(collection, x, extra_msg = "")
24:       collection = [collection] unless collection.is_a?(Array)
25:       msg = "#{x.inspect} not found in #{collection.to_a.inspect} #{extra_msg}"
26:       case x
27:       when Regexp
28:         assert(collection.detect { |e| e =~ x }, msg)
29:       else         
30:         assert(collection.include?(x), msg)
31:       end
32:     end

Asserts that the given collection does not contain item x. If x is a regular expression, ensure that none of the elements from the collection match x.

[Source]

    # File lib/shoulda/assertions.rb, line 36
36:     def assert_does_not_contain(collection, x, extra_msg = "")
37:       collection = [collection] unless collection.is_a?(Array)
38:       msg = "#{x.inspect} found in #{collection.to_a.inspect} " + extra_msg
39:       case x
40:       when Regexp
41:         assert(!collection.detect { |e| e =~ x }, msg)
42:       else         
43:         assert(!collection.include?(x), msg)
44:       end
45:     end

Asserts that the given matcher returns false when target is passed to matches?

[Source]

    # File lib/shoulda/assertions.rb, line 60
60:     def assert_rejects(matcher, target, options = {})
61:       unless matcher.matches?(target)
62:         assert_block { true }
63:         if options[:message]
64:           assert_match options[:message], matcher.failure_message
65:         end
66:       else
67:         assert_block(matcher.negative_failure_message) { false }
68:       end
69:     end

Asserts that two arrays contain the same elements, the same number of times. Essentially ==, but unordered.

  assert_same_elements([:a, :b, :c], [:c, :a, :b]) => passes

[Source]

    # File lib/shoulda/assertions.rb, line 6
 6:     def assert_same_elements(a1, a2, msg = nil)
 7:       [:select, :inject, :size].each do |m|
 8:         [a1, a2].each {|a| assert_respond_to(a, m, "Are you sure that #{a.inspect} is an array?  It doesn't respond to #{m}.") }
 9:       end
10: 
11:       assert a1h = a1.inject({}) { |h,e| h[e] = a1.select { |i| i == e }.size; h }
12:       assert a2h = a2.inject({}) { |h,e| h[e] = a2.select { |i| i == e }.size; h }
13: 
14:       assert_equal(a1h, a2h, msg)
15:     end

[Validate]