Class: FlexMock::Recorder

Inherits:
Object show all
Includes:
ArgumentTypes
Defined in:
lib/flexmock/recorder.rb

Overview

Translate arbitrary method calls into expectations on the given mock object.

Instance Method Summary collapse

Methods included from ArgumentTypes

#any, #ducktype, #eq, #hsh, #on, #optional_proc

Constructor Details

#initialize(mock) ⇒ Recorder

Create a method recorder for the mock mock.



23
24
25
26
# File 'lib/flexmock/recorder.rb', line 23

def initialize(mock)
  @mock = mock
  @strict = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Record an expectation for receiving the method sym with the given arguments.



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/flexmock/recorder.rb', line 58

def method_missing(sym, *args, &block)
  expectation = @mock.
    flexmock_define_expectation(caller, sym).
    and_return(&block)
  if strict?
    args = args.collect { |arg| eq(arg) }
    expectation.with(*args).ordered.once
  else
    expectation.with(*args)
  end
  expectation
end

Instance Method Details

#should_be_strict(is_strict = true) ⇒ Object

Place the record in strict mode. While recording expectations in strict mode, the following will be true.

  • All expectations will be expected in the order they were recorded.

  • All expectations will be expected once.

  • All arguments will be placed in exact match mode, including regular expressions and class objects.

Strict mode is usually used when giving the recorder to a known good algorithm. Strict mode captures the exact sequence of calls and validate that the code under test performs the exact same sequence of calls.

The recorder may exit strict mode via a should_be_strict(false) call. Non-strict expectations may be recorded at that point, or even explicit expectations (using should_receieve) can be specified.



47
48
49
# File 'lib/flexmock/recorder.rb', line 47

def should_be_strict(is_strict=true)
  @strict = is_strict
end

#strict?Boolean

Is the recorder in strict mode?

Returns:

  • (Boolean)


52
53
54
# File 'lib/flexmock/recorder.rb', line 52

def strict?
  @strict
end