forked from freerange/mocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_of.rb
42 lines (32 loc) · 985 Bytes
/
instance_of.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
require 'mocha/parameter_matchers/base'
module Mocha
module ParameterMatchers
# :call-seq: instance_of(klass) -> parameter_matcher
#
# Matches any object that is an instance of +klass+
# object = mock()
# object.expects(:method_1).with(instance_of(String))
# object.method_1('string')
# # no error raised
#
# object = mock()
# object.expects(:method_1).with(instance_of(String))
# object.method_1(99)
# # error raised, because method_1 was not called with an instance of String
def instance_of(klass)
InstanceOf.new(klass)
end
class InstanceOf < Base # :nodoc:
def initialize(klass)
@klass = klass
end
def matches?(available_parameters)
parameter = available_parameters.shift
parameter.instance_of?(@klass)
end
def mocha_inspect
"instance_of(#{@klass.mocha_inspect})"
end
end
end
end