Module: Mock
- Defined in:
- lib/extensions/mspec/mspec/mocks/mock.rb
Class Method Summary collapse
- .cleanup ⇒ Object
- .clear_replaced(key) ⇒ Object
- .has_key?(keys, sym) ⇒ Boolean
- .install_method(obj, sym, type = nil) ⇒ Object
- .mock_respond_to?(obj, sym) ⇒ Boolean
- .mocks ⇒ Object
- .name_or_inspect(obj) ⇒ Object
- .objects ⇒ Object
- .replaced?(sym) ⇒ Boolean
- .replaced_key(obj, sym) ⇒ Object
- .replaced_name(obj, sym) ⇒ Object
- .reset ⇒ Object
- .stubs ⇒ Object
- .verify_call(obj, sym, *args, &block) ⇒ Object
- .verify_count ⇒ Object
Class Method Details
.cleanup ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/extensions/mspec/mspec/mocks/mock.rb', line 178 def self.cleanup objects.each do |key, obj| if obj.kind_of? MockIntObject clear_replaced key next end replaced = key.first sym = key.last = obj.singleton_class if mock_respond_to? obj, replaced .__send__ :alias_method, sym, replaced .__send__ :remove_method, replaced else .__send__ :remove_method, sym end clear_replaced key end ensure reset end |
.clear_replaced(key) ⇒ Object
41 42 43 44 |
# File 'lib/extensions/mspec/mspec/mocks/mock.rb', line 41 def self.clear_replaced(key) mocks.delete key stubs.delete key end |
.has_key?(keys, sym) ⇒ Boolean
33 34 35 |
# File 'lib/extensions/mspec/mspec/mocks/mock.rb', line 33 def self.has_key?(keys, sym) !!keys.find { |k| k.first == sym } end |
.install_method(obj, sym, type = nil) ⇒ Object
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 |
# File 'lib/extensions/mspec/mspec/mocks/mock.rb', line 55 def self.install_method(obj, sym, type=nil) = obj.singleton_class key = replaced_key obj, sym sym = sym.to_sym if (sym == :respond_to? or mock_respond_to?(obj, sym)) and !replaced?(key.first) .__send__ :alias_method, key.first, sym end .class_eval <<-END def #{sym}(*args, &block) Mock.verify_call self, :#{sym}, *args, &block end END proxy = MockProxy.new type if proxy.mock? MSpec.expectation MSpec.actions :expectation, MSpec.current.state end if proxy.stub? stubs[key].unshift proxy else mocks[key] << proxy end objects[key] = obj proxy end |
.mock_respond_to?(obj, sym) ⇒ Boolean
46 47 48 49 50 51 52 53 |
# File 'lib/extensions/mspec/mspec/mocks/mock.rb', line 46 def self.mock_respond_to?(obj, sym) name = replaced_name(obj, :respond_to?) if replaced? name obj.__send__ name, sym else obj.respond_to? sym end end |
.mocks ⇒ Object
17 18 19 |
# File 'lib/extensions/mspec/mspec/mocks/mock.rb', line 17 def self.mocks @mocks ||= Hash.new { |h,k| h[k] = [] } end |
.name_or_inspect(obj) ⇒ Object
88 89 90 |
# File 'lib/extensions/mspec/mspec/mocks/mock.rb', line 88 def self.name_or_inspect(obj) obj.instance_variable_get(:@name) || obj.inspect end |
.objects ⇒ Object
13 14 15 |
# File 'lib/extensions/mspec/mspec/mocks/mock.rb', line 13 def self.objects @objects ||= {} end |
.replaced?(sym) ⇒ Boolean
37 38 39 |
# File 'lib/extensions/mspec/mspec/mocks/mock.rb', line 37 def self.replaced?(sym) has_key?(mocks.keys, sym) or has_key?(stubs.keys, sym) end |
.replaced_key(obj, sym) ⇒ Object
29 30 31 |
# File 'lib/extensions/mspec/mspec/mocks/mock.rb', line 29 def self.replaced_key(obj, sym) [replaced_name(obj, sym), sym] end |
.replaced_name(obj, sym) ⇒ Object
25 26 27 |
# File 'lib/extensions/mspec/mspec/mocks/mock.rb', line 25 def self.replaced_name(obj, sym) :"__mspec_#{obj.__mspec_object_id__}_#{sym}__" end |
.reset ⇒ Object
9 10 11 |
# File 'lib/extensions/mspec/mspec/mocks/mock.rb', line 9 def self.reset @mocks = @stubs = @objects = nil end |
.stubs ⇒ Object
21 22 23 |
# File 'lib/extensions/mspec/mspec/mocks/mock.rb', line 21 def self.stubs @stubs ||= Hash.new { |h,k| h[k] = [] } end |
.verify_call(obj, sym, *args, &block) ⇒ Object
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 |
# File 'lib/extensions/mspec/mspec/mocks/mock.rb', line 119 def self.verify_call(obj, sym, *args, &block) compare = *args behaves_like_ruby_1_9 = *[] if (behaves_like_ruby_1_9) compare = compare.first if compare.length <= 1 end key = replaced_key obj, sym [mocks, stubs].each do |proxies| proxies[key].each do |proxy| pass = case proxy.arguments when :any_args true when :no_args compare.nil? else proxy.arguments == compare end if proxy.yielding? if block proxy.yielding.each do |args_to_yield| if block.arity == -1 || block.arity == args_to_yield.size block.call(*args_to_yield) else SpecExpectation.fail_with( "Mock '#{name_or_inspect obj}' asked to yield " \ "|#{proxy.yielding.join(', ')}| on #{sym}\n", "but a block with arity #{block.arity} was passed") end end else SpecExpectation.fail_with( "Mock '#{name_or_inspect obj}' asked to yield " \ "|[#{proxy.yielding.join('], [')}]| on #{sym}\n", "but no block was passed") end end if pass proxy.called if proxy.raising? raise proxy.raising else return proxy.returning end end end end if sym.to_sym == :respond_to? mock_respond_to? obj, compare else SpecExpectation.fail_with("Mock '#{name_or_inspect obj}': method #{sym}\n", "called with unexpected arguments (#{Array(compare).join(' ')})") end end |
.verify_count ⇒ Object
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 |
# File 'lib/extensions/mspec/mspec/mocks/mock.rb', line 92 def self.verify_count mocks.each do |key, proxies| obj = objects[key] proxies.each do |proxy| qualifier, count = proxy.count pass = case qualifier when :at_least proxy.calls >= count when :at_most proxy.calls <= count when :exactly proxy.calls == count when :any_number_of_times true else false end unless pass SpecExpectation.fail_with( "Mock '#{name_or_inspect obj}' expected to receive '#{key.last}' " \ "#{qualifier.to_s.sub('_', ' ')} #{count} times", "but received it #{proxy.calls} times") end end end end |