Module: MicroMock

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

Overview

MicroMock is a tiny mocking script.

Constant Summary collapse

VERSION =
"1.1.0"

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

#attrs(*names) ⇒ Object

Creates attributes for a list of names.

Parameters:

  • names (Symbol)

    The list of attribute names.



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

def attrs(*names)
  names.each { |name| attr(name) }
end

#def(name) { ... } ⇒ Object

Creates a method.

Parameters:

  • name (Symbol)

    The name of the method.

Yields:

  • The block that will serve as the method body.



39
40
41
# File 'lib/micro_mock.rb', line 39

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