Class: FlexMock::ExpectationBuilder

### Home Classes Methods

### In Files

- lib/flexmock/expectation_builder.rb

### Parent

Object

### Methods

- #check_method_names
- #check_proper_mock
- #create_demeter_chain
- #create_expectation

### Files

- CHANGES
- GoogleExample.rdoc
- rspec_examples_spec.rdoc
- test_unit_examples_test.rdoc
- index.rdoc
- flexmock-0.4.0.rdoc
- flexmock-0.4.1.rdoc
- flexmock-0.4.2.rdoc
- flexmock-0.4.3.rdoc
- flexmock-0.5.0.rdoc
- flexmock-0.5.1.rdoc
- flexmock-0.6.0.rdoc
- flexmock-0.6.1.rdoc
- flexmock-0.6.2.rdoc
- flexmock-0.6.3.rdoc
- flexmock-0.6.4.rdoc
- flexmock-0.7.0.rdoc
- flexmock-0.7.1.rdoc
- flexmock-0.8.0.rdoc
- flexmock-0.8.2.rdoc
- flexmock-0.8.3.rdoc
- flexmock-0.8.4.rdoc
- flexmock-0.8.5.rdoc
- flexmock-0.9.0.rdoc
- flexmock-1.0.0.rdoc
- flexmock-1.0.3.rdoc
- flexmock-1.0.4.rdoc
- flexmock-1.1.0.rdoc
- flexmock-1.2.0.rdoc
- flexmock-1.3.0.rdoc
- flexmock-1.3.1.rdoc

### Class/Module Index

Quicksearch

- FlexMock
- FlexMock::AnyMatcher
- FlexMock::ArgumentMatching
- FlexMock::ArgumentTypes
- FlexMock::AtLeastCountValidator
- FlexMock::AtMostCountValidator
- FlexMock::CallValidator
- FlexMock::CompositeExpectation
- FlexMock::CountValidator
- FlexMock::DefaultFrameworkAdapter
- FlexMock::DefaultFrameworkAdapter::AssertionFailedError
- FlexMock::DuckMatcher
- FlexMock::EqualMatcher
- FlexMock::ExactCountValidator
- FlexMock::Expectation
- FlexMock::ExpectationBuilder
- FlexMock::ExpectationDirector
- FlexMock::ExpectationRecorder
- FlexMock::ExplicitNeeded
- FlexMock::ExtensionRegistry
- FlexMock::Extensions
- FlexMock::Extensions::ActiveRecordModel
- FlexMock::GenericTestCase
- FlexMock::HashMatcher
- FlexMock::MockBuilder
- FlexMock::MockContainer
- FlexMock::MockError
- FlexMock::OptionalProcMatcher
- FlexMock::Ordering
- FlexMock::PartialMockProxy
- FlexMock::ProcMatcher
- FlexMock::RSpecFrameworkAdapter
- FlexMock::RSpecFrameworkAdapter::AssertionFailedError
- FlexMock::RSpecMatchers
- FlexMock::RSpecMatchers::HaveReceived
- FlexMock::Recorder
- FlexMock::SpyDescribers
- FlexMock::TestCase
- FlexMock::TestUnitAssertions
- FlexMock::TestUnitFrameworkAdapter
- FlexMock::Undefined
- FlexMock::UsageError
- FlexMock::UseContainer
- FlexMock::Version
- MiniTest
- MiniTest::Unit
- MiniTest::Unit::TestCase
- Test
- Test::Unit
- Test::Unit::TestCase
- Class
- Module
- Object
- Symbol

No matching classes.

# 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 124
    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 101
    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 83
    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 50
    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 "Empty list of names"
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.