Module: SignalApi::ApiMock::ClassMethods

Defined in:
lib/signal_api/mocks/api_mock.rb

Constant Summary collapse

@@mock_method_definitions =
{}
@@mock_method_calls =
{}

Instance Method Summary collapse

Instance Method Details

#clear_mock_dataObject



64
65
66
# File 'lib/signal_api/mocks/api_mock.rb', line 64

def clear_mock_data
  @@mock_method_calls.keys.each { |k| @@mock_method_calls[k] = [] }
end

#mock_class_method(method, *parameter_names) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/signal_api/mocks/api_mock.rb', line 41

def mock_class_method(method, *parameter_names)
  @@mock_method_definitions[method] = parameter_names
  @@mock_method_calls[method] = []

  (class << self; self; end).instance_eval do
    define_method method do |*args|
      method_args = mock_method_definitions[method]

      called_args = {}
      method_args.each_with_index do |method_arg, i|
        called_args[method_arg] = args[i]
      end

      additional_info_method = method.to_s + "_additional_info"
      if method_defined?(additional_info_method)
        called_args.merge!(send(additional_info_method))
      end

      mock_method_calls[method] << called_args
    end
  end
end

#mock_method(method, *parameter_names) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/signal_api/mocks/api_mock.rb', line 20

def mock_method(method, *parameter_names)
  @@mock_method_definitions[method] = parameter_names
  @@mock_method_calls[method] = []

  define_method method do |*args|
    method_args = self.class.mock_method_definitions[method]

    called_args = {}
    method_args.each_with_index do |method_arg, i|
      called_args[method_arg] = args[i]
    end

    additional_info_method = method.to_s + "_additional_info"
    if self.class.method_defined?(additional_info_method)
      called_args.merge!(send(additional_info_method))
    end

    self.class.mock_method_calls[method] << called_args
  end
end

#mock_method_callsObject



12
13
14
# File 'lib/signal_api/mocks/api_mock.rb', line 12

def mock_method_calls
  @@mock_method_calls
end

#mock_method_definitionsObject



16
17
18
# File 'lib/signal_api/mocks/api_mock.rb', line 16

def mock_method_definitions
  @@mock_method_definitions
end