Class: FlexMock

Inherits:
Object show all
Extended by:
ArgumentTypes
Includes:
Ordering
Defined in:
lib/gems/flexmock-0.8.3/lib/flexmock/core.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/rspec.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/errors.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/ordering.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/recorder.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/composite.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/undefined.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/validators.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/expectation.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/partial_mock.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/argument_types.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/mock_container.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/argument_matchers.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/core_class_methods.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/deprecated_methods.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/rails/view_mocking.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/expectation_director.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/test_unit_integration.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/default_framework_adapter.rb

Overview

Deprecated Methods

The following methods are no longer supported in FlexMock. Include this file for legacy applications.

Defined Under Namespace

Modules: ArgumentTypes, MockContainer, Ordering, TestCase Classes: AnyMatcher, AtLeastCountValidator, AtMostCountValidator, CompositeExpectation, CountValidator, DefaultFrameworkAdapter, EqualMatcher, ExactCountValidator, Expectation, ExpectationDirector, ExpectationRecorder, MockContainerHelper, MockError, PartialMockProxy, ProcMatcher, RSpecFrameworkAdapter, Recorder, TestUnitFrameworkAdapter, Undefined, UsageError, UseContainer

Constant Summary collapse

ContainerHelper =
MockContainerHelper.new
ANY =
AnyMatcher.new

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ArgumentTypes

any, eq, on

Methods included from Ordering

#flexmock_allocate_order, #flexmock_current_order, #flexmock_current_order=, #flexmock_groups, #flexmock_validate_order

Constructor Details

#initialize(name = "unknown", container = nil) ⇒ FlexMock

Create a FlexMock object with the given name. The name is used in error messages. If no container is given, create a new, one-off container for this mock.



55
56
57
58
59
60
61
62
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core.rb', line 55

def initialize(name="unknown", container=nil)
  @flexmock_name = name
  @expectations = Hash.new
  @ignore_missing = false
  @verified = false
  container = UseContainer.new if container.nil?
  container.flexmock_remember(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Handle missing methods by attempting to look up a handler.



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core.rb', line 97

def method_missing(sym, *args, &block)
  flexmock_wrap do
    if handler = @expectations[sym]
      args << block  if block_given?
      handler.call(*args)
    elsif @ignore_missing
      FlexMock.undefined
    else
      super(sym, *args, &block)
    end
  end
end

Class Attribute Details

.framework_adapterObject (readonly)

Returns the value of attribute framework_adapter.



17
18
19
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core_class_methods.rb', line 17

def framework_adapter
  @framework_adapter
end

Instance Attribute Details

#flexmock_containerObject

Returns the value of attribute flexmock_container.



50
51
52
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core.rb', line 50

def flexmock_container
  @flexmock_container
end

#flexmock_nameObject (readonly)

Returns the value of attribute flexmock_name.



49
50
51
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core.rb', line 49

def flexmock_name
  @flexmock_name
end

Class Method Details

.check(msg, &block) ⇒ Object

Check will assert the block returns true. If it doesn’t, an assertion failure is triggered with the given message.



63
64
65
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core_class_methods.rb', line 63

def check(msg, &block)  # :nodoc:
  FlexMock.framework_adapter.assert_block(msg, &block)
end

.format_args(sym, args) ⇒ Object

Class method to format a method name and argument list as a nice looking string.



53
54
55
56
57
58
59
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core_class_methods.rb', line 53

def format_args(sym, args)  # :nodoc:
  if args
    "#{sym}(#{args.collect { |a| a.inspect }.join(', ')})"
  else
    "#{sym}(*args)"
  end
end

.undefinedObject

Undefined is normally available as FlexMock.undefined



43
44
45
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/undefined.rb', line 43

def self.undefined
  @undefined
end

.use(*names) ⇒ Object

Class method to make sure that verify is called at the end of a test. One mock object will be created for each name given to the use method. The mocks will be passed to the block as arguments. If no names are given, then a single anonymous mock object will be created.

At the end of the use block, each mock object will be verified to make sure the proper number of calls have been made.

Usage:

FlexMock.use("name") do |mock|    # Creates a mock named "name"
  mock.should_receive(:meth).
    returns(0).once
end                               # mock is verified here

NOTE: If you include FlexMock::TestCase into your test case file, you can create mocks that will be automatically verified in the test teardown by using the flexmock method.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core_class_methods.rb', line 39

def use(*names)
  names = ["unknown"] if names.empty?
  container = UseContainer.new
  mocks = names.collect { |n| container.flexmock(n) }
  yield(*mocks)
rescue Exception => ex
  container.got_exception = true
  raise
ensure
  container.flexmock_teardown
end

Instance Method Details

#by_defaultObject



91
92
93
94
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core.rb', line 91

def by_default
  @last_expectation.by_default
  self
end

#flexmock_expectations_for(method_name) ⇒ Object

Return the expectation director for a method name.



125
126
127
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core.rb', line 125

def flexmock_expectations_for(method_name) # :nodoc:
  @expectations[method_name]
end

#flexmock_find_expectation(method_name, *args) ⇒ Object

Find the mock expectation for method sym and arguments.



119
120
121
122
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core.rb', line 119

def flexmock_find_expectation(method_name, *args) # :nodoc:
  exp = @expectations[method_name]
  exp ? exp.find_expectation(*args) : nil
end

#flexmock_respond_to?Object

Save the original definition of respond_to? for use a bit later.



111
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core.rb', line 111

alias flexmock_respond_to? respond_to?

#flexmock_teardownObject

Teardown and infrastructure setup for this mock.



82
83
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core.rb', line 82

def flexmock_teardown
end

#flexmock_verifyObject

Verify that each method that had an explicit expected count was actually called that many times.



71
72
73
74
75
76
77
78
79
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core.rb', line 71

def flexmock_verify
  return if @verified
  @verified = true
  flexmock_wrap do
    @expectations.each do |sym, handler|
      handler.flexmock_verify
    end
  end
end

#inspectObject

Return the inspection string for a mock.



65
66
67
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core.rb', line 65

def inspect
  "<FlexMock:#{flexmock_name}>"
end

#method(sym) ⇒ Object

Override the built-in method to include the mocked methods.



130
131
132
133
134
135
136
137
138
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core.rb', line 130

def method(sym)
  @expectations[sym] || super
rescue NameError => ex
  if @ignore_missing
    proc { FlexMock.undefined }
  else
    raise ex
  end
end

#mock_handle(sym, expected_count = nil, &block) ⇒ Object

Handle all messages denoted by sym by calling the given block and passing any parameters to the block. If we know exactly how many calls are to be made to a particular method, we may check that by passing in the number of expected calls as a second paramter.



40
41
42
43
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/deprecated_methods.rb', line 40

def mock_handle(sym, expected_count=nil, &block) # :nodoc:
  $stderr.puts "mock_handle is deprecated, use the new should_receive interface instead."
  self.should_receive(sym).times(expected_count).returns(&block)
end

#respond_to?(sym, *args) ⇒ Boolean

Override the built-in respond_to? to include the mocked methods.

Returns:

  • (Boolean)


114
115
116
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core.rb', line 114

def respond_to?(sym, *args)
  super || (@expectations[sym] ? true : @ignore_missing)
end

#should_expect {|Recorder.new(self)| ... } ⇒ Object

Declare that the mock object should expect methods by providing a recorder for the methods and having the user invoke the expected methods in a block. Further expectations may be applied the result of the recording call.

Example Usage:

mock.should_expect do |record|
  record.add(Integer, 4) { |a, b|
    a + b
  }.at_least.once

Yields:



182
183
184
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core.rb', line 182

def should_expect
  yield Recorder.new(self)
end

#should_ignore_missingObject Also known as: mock_ignore_missing

Ignore all undefined (missing) method calls.



86
87
88
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core.rb', line 86

def should_ignore_missing
  @ignore_missing = true
end

#should_receive(*args) ⇒ Object

:call-seq:

mock.should_receive(:method_name)
mock.should_receive(:method1, method2, ...)
mock.should_receive(:meth1 => result1, :meth2 => result2, ...)

Declare that the mock object should receive a message with the given name.

If more than one method name is given, then the mock object should expect to receive all the listed melthods. If a hash of method name/value pairs is given, then the each method will return the associated result. Any expectations applied to the result of should_receive will be applied to all the methods defined in the argument list.

An expectation object for the method name is returned as the result of this method. Further expectation constraints can be added by chaining to the result.

See Expectation for a list of declarators that can be used.



159
160
161
162
163
164
165
166
167
168
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/core.rb', line 159

def should_receive(*args)
  @last_expectation = ContainerHelper.parse_should_args(self, args) do |sym|
    @expectations[sym] ||= ExpectationDirector.new(sym)
    result = Expectation.new(self, sym)
    @expectations[sym] << result
    override_existing_method(sym) if flexmock_respond_to?(sym)
    result
  end
  @last_expectation
end