Class: EMDextras::Spec::Spy

Inherits:
Object
  • Object
show all
Defined in:
lib/em-dextras/spec/spy.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Spy

Returns a new instance of Spy.



5
6
7
8
9
# File 'lib/em-dextras/spec/spy.rb', line 5

def initialize(options = {})
  @calls = []
  @return_value = options[:default_return]
  @only_respond_to = options[:only_respond_to]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



36
37
38
39
# File 'lib/em-dextras/spec/spy.rb', line 36

def method_missing(method_name, *args, &block)
  @calls << { :name => method_name, :args => args }
  @return_value
end

Instance Method Details

#called?(method_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/em-dextras/spec/spy.rb', line 11

def called?(method_name, *args)
  count_calls(method_name, *args) > 0
end

#not_received_call!(method_name, *args) ⇒ Object



28
29
30
# File 'lib/em-dextras/spec/spy.rb', line 28

def not_received_call!(method_name, *args)
  received_n_calls!(0, method_name, *args)
end

#received_call!(method_name, *args) ⇒ Object



24
25
26
# File 'lib/em-dextras/spec/spy.rb', line 24

def received_call!(method_name, *args)
  received_n_calls!(1, method_name, *args)
end

#received_n_calls!(number, method_name, *args) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/em-dextras/spec/spy.rb', line 15

def received_n_calls!(number, method_name, *args)
  probe_event_machine check: (Proc.new do
    received_calls_number = count_calls(method_name, *args)
    unless (received_calls_number == number )
      raise ExpectationFailed, "Expected #{method_name} to have been called #{number} times with parameters [#{args.join(",")}] but only received #{received_calls_number} such calls (also received the following calls: #{@calls.inspect})"
    end
  end)
end

#respond_to?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/em-dextras/spec/spy.rb', line 32

def respond_to?(symbol)
  @only_respond_to ? @only_respond_to.include?(symbol) : true
end