1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
require 'test_helper'
class HelpersTest < Test::Unit::TestCase # :nodoc:
context "an array of values" do
setup do
@a = ['abc', 'def', 3]
end
[/b/, 'abc', 3].each do |x|
should "contain #{x.inspect}" do
assert_raises(Test::Unit::AssertionFailedError) do
assert_does_not_contain @a, x
end
assert_contains @a, x
end
end
should "not contain 'wtf'" do
assert_raises(Test::Unit::AssertionFailedError) {assert_contains @a, 'wtf'}
assert_does_not_contain @a, 'wtf'
end
should "be the same as another array, ordered differently" do
assert_same_elements(@a, [3, "def", "abc"])
assert_raises(Test::Unit::AssertionFailedError) do
assert_same_elements(@a, [3, 3, "def", "abc"])
end
assert_raises(Test::Unit::AssertionFailedError) do
assert_same_elements([@a, "abc"].flatten, [3, 3, "def", "abc"])
end
end
end
context "a matching matcher" do
setup do
@matcher = stub('matcher', :matches? => true,
:failure_message => 'bad failure message',
:negative_failure_message => 'big time failure')
end
should "pass when given to assert_accepts with no message expectation" do
assert_accepts @matcher, 'target'
end
should "pass when given to assert_accepts with a matching message" do
assert_accepts @matcher, 'target', :message => /big time/
end
should "fail when given to assert_accepts with non-matching message" do
assert_raise Test::Unit::AssertionFailedError do
assert_accepts @matcher, 'target', :message => /small time/
end
end
context "when given to assert_rejects" do
setup do
begin
assert_rejects @matcher, 'target'
rescue Test::Unit::AssertionFailedError => @error
end
end
should "fail" do
assert_not_nil @error
end
should "use the error message from the matcher" do
assert_equal 'big time failure', @error.message
end
end
end
context "a non-matching matcher" do
setup do
@matcher = stub('matcher', :matches? => false,
:failure_message => 'big time failure',
:negative_failure_message => 'bad failure message')
end
should "pass when given to assert_rejects with no message expectation" do
assert_rejects @matcher, 'target'
end
should "pass when given to assert_rejects with a matching message" do
assert_rejects @matcher, 'target', :message => /big time/
end
should "fail when given to assert_rejects with a non-matching message" do
assert_raise Test::Unit::AssertionFailedError do
assert_rejects @matcher, 'target', :message => /small time/
end
end
context "when given to assert_accepts" do
setup do
begin
assert_accepts @matcher, 'target'
rescue Test::Unit::AssertionFailedError => @error
end
end
should "fail" do
assert_not_nil @error
end
should "use the error message from the matcher" do
assert_equal 'big time failure', @error.message
end
end
end
should "assign context to a support matching on assert_accepts" do
matcher = stub('matcher', :matches? => true)
matcher.expects(:in_context).with(self)
assert_accepts matcher, nil
end
should "assign context to a support matching on assert_rejects" do
matcher = stub('matcher', :matches? => false)
matcher.expects(:in_context).with(self)
assert_rejects matcher, nil
end
end
|