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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
require File.expand_path('../../test_helper', __FILE__)
require 'method_definer'
require 'mocha/mock'
require 'mocha/class_method'
class ClassMethodTest < Test::Unit::TestCase
include Mocha
def test_should_hide_original_method
klass = Class.new { def self.method_x; end }
method = ClassMethod.new(klass, :method_x)
method.hide_original_method
assert_equal false, klass.respond_to?(:method_x)
end
def test_should_not_raise_error_hiding_method_that_isnt_defined
klass = Class.new
method = ClassMethod.new(klass, :method_x)
assert_nothing_raised { method.hide_original_method }
end
def test_should_not_raise_error_hiding_method_in_class_that_implements_method_called_method
klass = Class.new { def self.method; end }
method = ClassMethod.new(klass, :method)
assert_nothing_raised { method.hide_original_method }
end
def test_should_define_a_new_method_which_should_call_mocha_method_missing
klass = Class.new { def self.method_x; end }
mocha = build_mock
klass.define_instance_method(:mocha) { mocha }
mocha.expects(:method_x).with(:param1, :param2).returns(:result)
method = ClassMethod.new(klass, :method_x)
method.hide_original_method
method.define_new_method
result = klass.method_x(:param1, :param2)
assert_equal :result, result
assert mocha.__verified__?
end
def test_should_remove_new_method
klass = Class.new { def self.method_x; end }
method = ClassMethod.new(klass, :method_x)
method.remove_new_method
assert_equal false, klass.respond_to?(:method_x)
end
def test_should_restore_original_method
klass = Class.new { def self.method_x; :original_result; end }
method = ClassMethod.new(klass, :method_x)
method.hide_original_method
method.define_new_method
method.remove_new_method
method.restore_original_method
assert klass.respond_to?(:method_x)
assert_equal :original_result, klass.method_x
end
def test_should_restore_original_method_accepting_a_block_parameter
klass = Class.new { def self.method_x(&block); block.call if block_given? ; end }
method = ClassMethod.new(klass, :method_x)
method.hide_original_method
method.define_new_method
method.remove_new_method
method.restore_original_method
block_called = false
klass.method_x { block_called = true }
assert block_called
end
def test_should_not_restore_original_method_if_none_was_defined_in_first_place
klass = Class.new { def self.method_x; :new_result; end }
method = ClassMethod.new(klass, :method_x)
method.restore_original_method
assert_equal :new_result, klass.method_x
end
def test_should_call_hide_original_method
klass = Class.new { def self.method_x; end }
method = ClassMethod.new(klass, :method_x)
method.hide_original_method
method.define_instance_accessor(:hide_called)
method.replace_instance_method(:hide_original_method) { self.hide_called = true }
method.stub
assert method.hide_called
end
def test_should_call_define_new_method
klass = Class.new { def self.method_x; end }
method = ClassMethod.new(klass, :method_x)
method.define_instance_accessor(:define_called)
method.replace_instance_method(:define_new_method) { self.define_called = true }
method.stub
assert method.define_called
end
def test_should_call_remove_new_method
klass = Class.new { def self.method_x; end }
method = ClassMethod.new(klass, :method_x)
mocha = build_mock
klass.define_instance_method(:mocha) { mocha }
method.define_instance_accessor(:remove_called)
method.replace_instance_method(:remove_new_method) { self.remove_called = true }
method.unstub
assert method.remove_called
end
def test_should_call_restore_original_method
klass = Class.new { def self.method_x; end }
mocha = build_mock
klass.define_instance_method(:mocha) { mocha }
method = ClassMethod.new(klass, :method_x)
method.define_instance_accessor(:restore_called)
method.replace_instance_method(:restore_original_method) { self.restore_called = true }
method.unstub
assert method.restore_called
end
def test_should_call_mocha_unstub
klass = Class.new { def self.method_x; end }
method = ClassMethod.new(klass, :method_x)
method.replace_instance_method(:restore_original_method) { }
mocha = Class.new { class << self; attr_accessor :unstub_method; end; def self.unstub(method); self.unstub_method = method; end; }
mocha.define_instance_method(:any_expectations?) { true }
method.replace_instance_method(:mock) { mocha }
method.unstub
assert_equal mocha.unstub_method, :method_x
end
def test_should_call_stubbee_reset_mocha_if_no_expectations_remaining
klass = Class.new { def self.method_x; end }
method = ClassMethod.new(klass, :method_x)
method.replace_instance_method(:remove_new_method) { }
method.replace_instance_method(:restore_original_method) { }
mocha = Class.new
mocha.define_instance_method(:unstub) { |method_name| }
mocha.define_instance_method(:any_expectations?) { false }
method.replace_instance_method(:mock) { mocha }
stubbee = Class.new { attr_accessor :reset_mocha_called; def reset_mocha; self.reset_mocha_called = true; end; }.new
method.replace_instance_method(:stubbee) { stubbee }
method.unstub
assert stubbee.reset_mocha_called
end
def test_should_return_mock_for_stubbee
mocha = Object.new
stubbee = Object.new
stubbee.define_instance_accessor(:mocha) { mocha }
stubbee.mocha = nil
method = ClassMethod.new(stubbee, :method_name)
assert_equal stubbee.mocha, method.mock
end
def test_should_not_match_if_other_object_has_a_different_class
class_method = ClassMethod.new(Object.new, :method)
other_object = Object.new
assert !class_method.matches?(other_object)
end
def test_should_not_match_if_other_class_method_has_different_stubbee
stubbee_1 = Object.new
stubbee_2 = Object.new
class_method_1 = ClassMethod.new(stubbee_1, :method)
class_method_2 = ClassMethod.new(stubbee_2, :method)
assert !class_method_1.matches?(class_method_2)
end
def test_should_not_match_if_other_class_method_has_different_method
stubbee = Object.new
class_method_1 = ClassMethod.new(stubbee, :method_1)
class_method_2 = ClassMethod.new(stubbee, :method_2)
assert !class_method_1.matches?(class_method_2)
end
def test_should_match_if_other_class_method_has_same_stubbee_and_same_method_so_no_attempt_is_made_to_stub_a_method_twice
stubbee = Object.new
class_method_1 = ClassMethod.new(stubbee, :method)
class_method_2 = ClassMethod.new(stubbee, :method)
assert class_method_1.matches?(class_method_2)
end
def test_should_match_if_other_class_method_has_same_stubbee_and_same_method_but_stubbee_equal_method_lies_like_active_record_association_proxy
stubbee = Class.new do
def equal?(other); false; end
end.new
class_method_1 = ClassMethod.new(stubbee, :method)
class_method_2 = ClassMethod.new(stubbee, :method)
assert class_method_1.matches?(class_method_2)
end
private
def build_mock
Mock.new(nil)
end
end
|