Class: FlexMock::MockBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/flexmock/mock_builder.rb

Overview

This class contains helper methods for mock containers. Since MockContainer is a module that is designed to be mixed into other classes, (particularly testing framework test cases), we don’t want to pollute the method namespace of the class that mixes in MockContainer. So we have aggressively moved a number of MockContainer methods out of that class and into MockBuilder to isoloate the names.

Defined Under Namespace

Classes: FlexOpts

Instance Method Summary collapse

Constructor Details

#initialize(container) ⇒ MockBuilder

Returns a new instance of MockBuilder.



12
13
14
# File 'lib/flexmock/mock_builder.rb', line 12

def initialize(container)
  @container = container
end

Instance Method Details

#create_double(opts) ⇒ Object

Create the test double based on the args options.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/flexmock/mock_builder.rb', line 62

def create_double(opts)
  if opts.extended
    result = opts.extended.create(container, opts)
  elsif opts.domain_obj
    result = create_partial(opts)
  else
    result = create_mock(opts)
  end
  opts.mock ||= result
  result
end

#define_a_mock(location, *args, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/flexmock/mock_builder.rb', line 16

def define_a_mock(location, *args, &block)
  opts = parse_creation_args(args)
  if opts.safe_mode && ! block_given?
    raise UsageError, "a block is required in safe mode"
  end

  result = create_double(opts)
  flexmock_mock_setup(opts.mock, opts, location, &block)
  run_post_creation_hooks(opts, location)
  result
end

#flexmock_mock_setup(mock, opts, location) {|mock| ... } ⇒ Object

Setup the test double with its expections and such.

Yields:

  • (mock)


83
84
85
86
87
88
# File 'lib/flexmock/mock_builder.rb', line 83

def flexmock_mock_setup(mock, opts, location)
  mock.flexmock_based_on(opts.base_class) if opts.base_class
  mock.flexmock_define_expectation(location, opts.defs)
  yield(mock) if block_given?
  container.flexmock_remember(mock)
end

#parse_creation_args(args) ⇒ Object

Parse the list of flexmock() arguments and populate the opts object.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/flexmock/mock_builder.rb', line 39

def parse_creation_args(args)
  opts = FlexOpts.new
  while ! args.empty?
    case args.first
    when Symbol
      unless parse_create_symbol(args, opts)
        opts.name = args.shift.to_s
      end
    when String, Symbol
      opts.name = args.shift.to_s
    when Hash
      opts.defs = args.shift
    when FlexMock
      opts.mock = args.shift
    else
      opts.domain_obj = args.shift
    end
  end
  set_base_class(opts)
  opts
end

#run_post_creation_hooks(opts, location) ⇒ Object

Run any post creation hooks specified by an extension.



75
76
77
78
79
# File 'lib/flexmock/mock_builder.rb', line 75

def run_post_creation_hooks(opts, location)
  if opts.extended
    opts.extended.post_create(opts, location)
  end
end