Shoulda::Assertions

Public Instance Methods

assert_accepts(matcher, target, options = {}) click to toggle source

Asserts that the given matcher returns true when target is passed to #

    # File lib/shoulda/assertions.rb, line 48
48:     def assert_accepts(matcher, target, options = {})
49:       if matcher.respond_to?(:in_context)
50:         matcher.in_context(self)
51:       end
52: 
53:       if matcher.matches?(target)
54:         assert_block { true }
55:         if options[:message]
56:           assert_match options[:message], matcher.negative_failure_message
57:         end
58:       else
59:         assert_block(matcher.failure_message) { false }
60:       end
61:     end
assert_contains(collection, x, extra_msg = "") click to toggle source

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
    # 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
assert_does_not_contain(collection, x, extra_msg = "") click to toggle source

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.

    # 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
assert_rejects(matcher, target, options = {}) click to toggle source

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

    # File lib/shoulda/assertions.rb, line 64
64:     def assert_rejects(matcher, target, options = {})
65:       if matcher.respond_to?(:in_context)
66:         matcher.in_context(self)
67:       end
68: 
69:       unless matcher.matches?(target)
70:         assert_block { true }
71:         if options[:message]
72:           assert_match options[:message], matcher.failure_message
73:         end
74:       else
75:         assert_block(matcher.negative_failure_message) { false }
76:       end
77:     end
assert_same_elements(a1, a2, msg = nil) click to toggle source

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
    # 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

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.