Class: Spectro::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/spectro/mock.rb

Class Method Summary collapse

Class Method Details

.create(file_path, method_name) ⇒ NilClass, Proc

Creates a mock of the given method for the given file based on the spec rules. If mocks are not enabled it defaults to nil

Parameters:

  • file_path (String)

    relative path of the file that requests the lambda

  • method_name (Symbol)

    the method name that would be implemented

Returns:

  • (NilClass, Proc)

    the mock as Proc or nil if mocks are disabled



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/spectro/mock.rb', line 12

def self.create file_path, method_name
  return nil unless Spectro::Config.mocks_enabled?
 
  spec = Spectro::Spec::Parser.parse(file_path).detect do |spec|
    spec.signature.name == method_name.to_s
  end
  
  param_names = ('a'..'z').first(spec.signature.params_types.count)
  
  responses = spec.rules.inject({}) do |memo, rule|
    memo[rule.params] = rule.output  
    memo
  end
  
  return eval "
    ->(#{param_names.join(',')}) {
      if !responses.has_key?([#{param_names.join(',')}])
        raise Spectro::Exception::UnknownMockResponse.new(file_path, method_name)          
      end

      return responses[[#{param_names.join(',')}]] 
    }
  "
end