Module: FlexMock::MockContainer

Includes:
Ordering
Included in:
TestCase, UseContainer, Spec::Adapters::MockFramework, Test::Unit::TestCase
Defined in:
lib/gems/flexmock-0.8.3/lib/flexmock/mock_container.rb,
lib/gems/flexmock-0.8.3/lib/flexmock/rails/view_mocking.rb

Overview

###################################################################### Mock container methods

Include this module in to get integration with FlexMock. When this module is included, mocks may be created with a simple call to the flexmock method. Mocks created with via the method call will automatically be verified in the teardown of the test case.

Instance Method Summary collapse

Methods included from Ordering

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

Instance Method Details

#flexmock(*args) {|mock| ... } ⇒ Object Also known as: flexstub

Create a mocking object in the FlexMock framework. The flexmock method has a number of options available, depending on just what kind of mocking object your require. Mocks created via flexmock will be automatically verify during the teardown phase of your test framework.

:call-seq:

flexmock() { |mock| ... }
flexmock(name) { |mock| ... }
flexmock(expect_hash) { |mock| ... }
flexmock(name, expect_hash) { |mock| ... }
flexmock(real_object) { |mock| ... }
flexmock(real_object, name) { |mock| ... }
flexmock(real_object, name, expect_hash) { |mock| ... }
flexmock(:base, string, name, expect_hash) { |mock| ... }

Note: A plain flexmock() call without a block will return the mock object (the object that interprets should_receive and its brethern). A flexmock() call that includes a block will return the domain objects (the object that will interpret domain messages) since the mock will be passed to the block for configuration. With regular mocks, this distinction is unimportant because the mock object and the domain object are the same object. However, with partial mocks, the mock object is separation from the domain object. Keep that distinciton in mind.

name

Name of the mock object. If no name is given, “unknown” is used for full mocks and “flexmock(real_object)” is used for partial mocks.

expect_hash

Hash table of method names and values. Each method/value pair is used to setup a simple expectation so that if the mock object receives a message matching an entry in the table, it returns the associated value. No argument our call count constraints are added. Using an expect_hash is identical to calling:

mock.should_receive(method_name).and_return(value)

for each of the method/value pairs in the hash.

real_object

If a real object is given, then a partial mock is constructed using the real_object as a base. Partial mocks (formally referred to as stubs) behave as a mock object when an expectation is matched, and otherwise will behave like the original object. This is useful when you want to use a real object for testing, but need to mock out just one or two methods.

:base

Forces the following argument to be used as the base of a partial mock object. This explicit tag is only needed if you want to use a string or a symbol as the mock base (string and symbols would normally be interpretted as the mock name).

&block

If a block is given, then the mock object is passed to the block and expectations may be configured within the block. When a block is given for a partial mock, flexmock will return the domain object rather than the mock object.

Yields:

  • (mock)

Raises:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/mock_container.rb', line 115

def flexmock(*args)
  name = nil
  quick_defs = {}
  domain_obj = nil
  safe_mode = false
  model_class = nil
  while ! args.empty?
    case args.first
    when :base, :safe
      safe_mode = (args.shift == :safe)
      domain_obj = args.shift
    when :model
      args.shift
      model_class = args.shift
    when String, Symbol
      name = args.shift.to_s
    when Hash
      quick_defs = args.shift
    else
      domain_obj = args.shift
    end
  end
  raise UsageError, "a block is required in safe mode" if safe_mode && ! block_given?

  if domain_obj
    mock = ContainerHelper.make_partial_proxy(self, domain_obj, name, safe_mode)
    result = domain_obj
  elsif model_class
    id = ContainerHelper.next_id
    result = mock = FlexMock.new("#{model_class}_#{id}", self)
  else
    result = mock = FlexMock.new(name || "unknown", self)
  end
  mock.should_receive(quick_defs)
  yield(mock) if block_given?
  flexmock_remember(mock)
  ContainerHelper.add_model_methods(mock, model_class, id) if model_class
  result
end

#flexmock_closeObject

Close all the mock objects in the container. Closing a mock object restores any original behavior that was displaced by the mock.



46
47
48
49
50
51
52
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/mock_container.rb', line 46

def flexmock_close
  @flexmock_created_mocks ||= []
  @flexmock_created_mocks.each do |m|
    m.flexmock_teardown
  end
  @flexmock_created_mocks = []
end

#flexmock_remember(mocking_object) ⇒ Object

Remember the mock object / stub in the mock container.



157
158
159
160
161
162
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/mock_container.rb', line 157

def flexmock_remember(mocking_object)
  @flexmock_created_mocks ||= []
  @flexmock_created_mocks << mocking_object
  mocking_object.flexmock_container = self
  mocking_object
end

#flexmock_teardownObject

Do the flexmock specific teardown stuff. If you need finer control, you can use either flexmock_verify or flexmock_close.



30
31
32
33
34
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/mock_container.rb', line 30

def flexmock_teardown
  flexmock_verify if passed?
ensure
  flexmock_close
end

#flexmock_verifyObject

Perform verification on all mocks in the container.



37
38
39
40
41
42
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/mock_container.rb', line 37

def flexmock_verify
  @flexmock_created_mocks ||= []
  @flexmock_created_mocks.each do |m|
    m.flexmock_verify
  end
end

#rails_versionObject



6
7
8
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/rails/view_mocking.rb', line 6

def rails_version
  Rails::VERSION::STRING
end

#should_render_view(template_name = nil) ⇒ Object

Declare that the Rails controller under test should render the named view. If a view template name is given, it will be an error if the named view is not rendered during the execution of the contoller action. If no template name is given, then the any view may be rendered. If no view is actually rendered, then an assertion failure will occur.

def test_my_action_does_what_it_should
  should_render_view 'show'

  get :show, :id => 1

  assert_response :success
end


25
26
27
28
29
30
31
32
33
# File 'lib/gems/flexmock-0.8.3/lib/flexmock/rails/view_mocking.rb', line 25

def should_render_view(template_name=nil)
  if rails_version <= '1.2.4'
    should_render_view_prior_version_124(template_name)
  elsif rails_version <= '2.0.0'
    should_render_view_after_version_124(template_name)
  else
    should_render_view_after_version_202(template_name)
  end
end