-
-
Notifications
You must be signed in to change notification settings - Fork 357
/
mock.rb
81 lines (70 loc) · 2.26 KB
/
mock.rb
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
module RSpec
module Mocks
class Mock
include Methods
# Creates a new test double with a `name` (that will be used in error
# messages only)
def initialize(name=nil, stubs_and_options={})
if name.is_a?(Hash) && stubs_and_options.empty?
stubs_and_options = name
@name = nil
else
@name = name
end
@options = extract_options(stubs_and_options)
assign_stubs(stubs_and_options)
end
# This allows for comparing the mock to other objects that proxy such as
# ActiveRecords belongs_to proxy objects. By making the other object run
# the comparison, we're sure the call gets delegated to the proxy
# target.
def ==(other)
other == __mock_proxy
end
# @private
def inspect
"#<#{self.class}:#{sprintf '0x%x', self.object_id} @name=#{@name.inspect}>"
end
# @private
def to_s
inspect.gsub('<','[').gsub('>',']')
end
alias_method :to_str, :to_s
# @private
def respond_to?(message, incl_private=false)
__mock_proxy.null_object? && message != :to_ary ? true : super
end
private
def method_missing(message, *args, &block)
raise NoMethodError if message == :to_ary
__mock_proxy.record_message_received(message, *args, &block)
begin
__mock_proxy.null_object? ? self : super
rescue NameError
__mock_proxy.raise_unexpected_message_error(message, *args)
end
end
def extract_options(stubs_and_options)
if stubs_and_options[:null_object]
@null_object = stubs_and_options.delete(:null_object)
RSpec.deprecate(%Q["double('name', :null_object => true)"], %Q["double('name').as_null_object"])
end
options = {}
extract_option(stubs_and_options, options, :__declared_as, 'Mock')
options
end
def extract_option(source, target, key, default=nil)
if source[key]
target[key] = source.delete(key)
elsif default
target[key] = default
end
end
def assign_stubs(stubs)
stubs.each_pair do |message, response|
stub(message).and_return(response)
end
end
end
end
end