###################################################################### 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.

Methods
Included Modules
Public Instance methods
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| ... }

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.

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.
This method is also aliased as flexstub
     # File lib/flexmock/mock_container.rb, line 115
115:     def flexmock(*args)
116:       name = nil
117:       quick_defs = {}
118:       domain_obj = nil
119:       safe_mode = false
120:       model_class = nil
121:       while ! args.empty?
122:         case args.first
123:         when :base, :safe
124:           safe_mode = (args.shift == :safe)
125:           domain_obj = args.shift
126:         when :model
127:           args.shift
128:           model_class = args.shift
129:         when String, Symbol
130:           name = args.shift.to_s
131:         when Hash
132:           quick_defs = args.shift
133:         else
134:           domain_obj = args.shift
135:         end
136:       end
137:       raise UsageError, "a block is required in safe mode" if safe_mode && ! block_given?
138: 
139:       if domain_obj
140:         mock = ContainerHelper.make_partial_proxy(self, domain_obj, name, safe_mode)
141:         result = domain_obj
142:       elsif model_class
143:         id = ContainerHelper.next_id
144:         result = mock = FlexMock.new("#{model_class}_#{id}", self)
145:       else
146:         result = mock = FlexMock.new(name || "unknown", self)
147:       end
148:       mock.should_receive(quick_defs)
149:       yield(mock) if block_given?
150:       flexmock_remember(mock)
151:       ContainerHelper.add_model_methods(mock, model_class, id) if model_class
152:       result
153:     end
flexmock_close()

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

    # File lib/flexmock/mock_container.rb, line 46
46:     def flexmock_close
47:       @flexmock_created_mocks ||= []
48:       @flexmock_created_mocks.each do |m|
49:         m.flexmock_teardown
50:       end
51:       @flexmock_created_mocks = []
52:     end
flexmock_remember(mocking_object)

Remember the mock object / stub in the mock container.

     # File lib/flexmock/mock_container.rb, line 157
157:     def flexmock_remember(mocking_object)
158:       @flexmock_created_mocks ||= []
159:       @flexmock_created_mocks << mocking_object
160:       mocking_object.flexmock_container = self
161:       mocking_object
162:     end
flexmock_teardown()

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

    # File lib/flexmock/mock_container.rb, line 30
30:     def flexmock_teardown
31:       flexmock_verify if passed?
32:     ensure
33:       flexmock_close
34:     end
flexmock_verify()

Perform verification on all mocks in the container.

    # File lib/flexmock/mock_container.rb, line 37
37:     def flexmock_verify
38:       @flexmock_created_mocks ||= []
39:       @flexmock_created_mocks.each do |m|
40:         m.flexmock_verify
41:       end
42:     end
flexstub(*args)

Alias for flexmock

rails_version()
   # File lib/flexmock/rails/view_mocking.rb, line 6
6:     def rails_version
7:       Rails::VERSION::STRING
8:     end
should_render_view(template_name=nil)

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
    # File lib/flexmock/rails/view_mocking.rb, line 25
25:     def should_render_view(template_name=nil)
26:       if rails_version <= '1.2.4'
27:         should_render_view_prior_version_124(template_name)
28:       elsif rails_version <= '2.0.0'
29:         should_render_view_after_version_124(template_name)
30:       else
31:         should_render_view_after_version_202(template_name)
32:       end
33:     end