Module: DhEasy::Core::Mock

Defined in:
lib/dh_easy/core/mock.rb,
lib/dh_easy/core/mock/fake_db.rb,
lib/dh_easy/core/mock/fake_parser.rb,
lib/dh_easy/core/mock/fake_seeder.rb,
lib/dh_easy/core/mock/fake_executor.rb,
lib/dh_easy/core/mock/fake_finisher.rb

Defined Under Namespace

Modules: FakeExecutor Classes: FakeDb, FakeFinisher, FakeParser, FakeSeeder

Class Method Summary collapse

Class Method Details

.context_vars(exposed_methods) ⇒ Array

Generate a context and message queue from a list of exposed methods.

Examples:

exposed_methods = [:boo, :bar]
context, message_queue = DhEasy::Core::Mock.context_vars exposed_methods
context.boo 1, 2
context.bar 'A', 'B'
context.bar '111', '222'
message_queue
# => [
#   [:boo, [1, 2]],
#   [:bar, ['A', 'B']],
#   [:bar, ['111', '222']]
# ]

Parameters:

  • exposed_methods (Array)

    List of exposed methods.

Returns:

  • (Array)

    `[context, message_queue]` being:

    • `context`: Object implementing exposed methods.

    • `[Array] message_queue`: Array to store messages.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dh_easy/core/mock.rb', line 30

def self.context_vars exposed_methods
  context = Object.new
  metaclass = class << context; self; end
  message_queue = [] # Beat reference bug
  exposed_methods = exposed_methods
  exposed_methods.each do |key|
    metaclass.send(:define_method, key) do |*args|
      # Record all method calls into message queue for easy access
      message_queue << [key, args]
    end
  end
  [context, message_queue]
end