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