Module: MicroMock

Defined in:
lib/micro_mock.rb,
lib/micro_mock/version.rb

Overview

MicroMock is a tiny mocking script.

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.make(*ancestors) ⇒ Object

Defines a mock class.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/micro_mock.rb', line 5

def self.make(*ancestors)
  superclass = ancestors.shift if ancestors.first.is_a?(Class)
  superclass ||= Object
  mixins = ancestors
  klass = Class.new(superclass) do
    mixins.each { |mixin| send :include, mixin }
    def initialize(*args)
      @args = args
      super unless self.class.superclass == Object
    end
  end
  klass.extend MicroMock
  klass.send :include, MicroMock
  Object.const_set "MicroMock#{klass.object_id}", klass
  klass
end

Instance Method Details

#attr(name, default_value = nil) ⇒ Object

Creates an attribute getter & setter.

Parameters:

  • name (Symbol)

    The name of the attribute.

  • default_value (Object) (defaults to: nil)

    An optional default value.



25
26
27
28
# File 'lib/micro_mock.rb', line 25

def attr(name, default_value=nil)
  context.send :attr_accessor, name
  instance_variable_set "@#{name}", default_value
end

#meth(name) { ... } ⇒ Object Also known as: stub

Creates a method.

Parameters:

  • name (Symbol)

    The name of the method.

Yields:

  • The block that will serve as the method body.



33
34
35
# File 'lib/micro_mock.rb', line 33

def meth(name, &block)
  context.send :define_method, name, &block
end