class FlexMock::ExpectationBuilder

Constants

METHOD_NAME_ALTS
METHOD_NAME_RE

Public Instance Methods

check_method_names(names) click to toggle source

Check that all the names in the list are valid method names.

# File lib/flexmock/expectation_builder.rb, line 123
def check_method_names(names)
  names.each do |name|
    fail FlexMock::UsageError, "Ill-formed method name '#{name}'" if
      name.to_s !~ METHOD_NAME_RE
  end
end
check_proper_mock(mock, method_name) click to toggle source

Check that the given mock is a real FlexMock mock.

# File lib/flexmock/expectation_builder.rb, line 100
def check_proper_mock(mock, method_name)
  unless mock.respond_to?(:should_receive)
    fail FlexMock::UsageError,
      "Conflicting mock declaration for '#{method_name}' in demeter style mock"
  end
end
create_demeter_chain(mock, names) click to toggle source

Build the chain of mocks for demeter style mocking.

This method builds a chain of mocks to support demeter style mocking. Given a mock chain of “first.second.third.last”, we must build a chain of mock methods that return the next mock in the chain. The expectation for the last method of the chain is returned as the result of the method.

Things to consider:

  • The expectations for all methods but the last in the chain will be setup to expect no parameters and to return the next mock in the chain.

  • It could very well be the case that several demeter chains will be defined on a single mock object, and those chains could share some of the same methods (e.g. “mock.one.two.read” and “mock.one.two.write” both share the methods “one” and “two”). It is important that the shared methods return the same mocks in both chains.

# File lib/flexmock/expectation_builder.rb, line 82
def create_demeter_chain(mock, names)
  container = mock.flexmock_container
  last_method = names.pop
  names.each do |name|
    exp = mock.flexmock_find_expectation(name)
    if exp
      next_mock = exp._return_value([])
      check_proper_mock(next_mock, name)
    else
      next_mock = container.flexmock("demeter_#{name}")
      mock.should_receive(name).and_return(next_mock)
    end
    mock = next_mock
  end
  mock.should_receive(last_method)
end
create_expectation(mock, name_chain, &block) click to toggle source

Create an expectation for the name on this mock. For simple mocks, this is done by calling the provided block parameter and letting the calling site handle the creation of the expectation (which differs between full mocks and partial mocks).

If the name_chain contains demeter mocking chains, then the process is more complex. A series of mocks are created, each component of the chain returning the next mock until the expectation for the last component is returned.

# File lib/flexmock/expectation_builder.rb, line 49
def create_expectation(mock, name_chain, &block)
  names = name_chain.to_s.split('.').map { |n| n.to_sym }
  check_method_names(names)
  if names.size == 1
    block.call(names.first)
  elsif names.size > 1
    create_demeter_chain(mock, names)
  else
    fail ArgumentError, "Empty list of names"
  end
end