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
# 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
  end
  param_names = Array.new(spec.signature.params_types.count) do |index|
    ('a'.ord+index).chr
  end
  responses = spec.rules.inject({}) do |memo, rule|
    memo[rule.params] = rule.output  
    memo
  end
  return eval "
    lambda do |#{param_names.join(',')}|
      return responses[[#{param_names.join(',')}]] 
    end
  "
end