###################################################################### 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.
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. |
# File lib/flexmock/mock_container.rb, line 119 119: def flexmock(*args) 120: name = nil 121: quick_defs = {} 122: domain_obj = nil 123: safe_mode = false 124: model_class = nil 125: while ! args.empty? 126: case args.first 127: when :base, :safe 128: safe_mode = (args.shift == :safe) 129: domain_obj = args.shift 130: when :model 131: args.shift 132: model_class = args.shift 133: when String, Symbol 134: name = args.shift.to_s 135: when Hash 136: quick_defs = args.shift 137: else 138: domain_obj = args.shift 139: end 140: end 141: raise UsageError, "a block is required in safe mode" if safe_mode && ! block_given? 142: 143: if domain_obj 144: mock = ContainerHelper.make_partial_proxy(self, domain_obj, name, safe_mode) 145: result = domain_obj 146: elsif model_class 147: id = ContainerHelper.next_id 148: result = mock = FlexMock.new("#{model_class}_#{id}", self) 149: else 150: result = mock = FlexMock.new(name || "unknown", self) 151: end 152: mock.should_receive(quick_defs) 153: yield(mock) if block_given? 154: flexmock_remember(mock) 155: ContainerHelper.add_model_methods(mock, model_class, id) if model_class 156: result 157: end
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 51 51: def flexmock_close 52: flexmock_created_mocks.each do |m| 53: m.flexmock_teardown 54: end 55: @flexmock_created_mocks = [] 56: end
List of mocks created in this container
# File lib/flexmock/mock_container.rb, line 45 45: def flexmock_created_mocks 46: @flexmock_created_mocks ||= [] 47: end
Remember the mock object / stub in the mock container.
# File lib/flexmock/mock_container.rb, line 161 161: def flexmock_remember(mocking_object) 162: @flexmock_created_mocks ||= [] 163: @flexmock_created_mocks << mocking_object 164: mocking_object.flexmock_container = self 165: mocking_object 166: end
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 31 31: def flexmock_teardown 32: flexmock_verify if passed? 33: ensure 34: flexmock_close 35: end
Perform verification on all mocks in the container.
# File lib/flexmock/mock_container.rb, line 38 38: def flexmock_verify 39: flexmock_created_mocks.each do |m| 40: m.flexmock_verify 41: end 42: end
# File lib/flexmock/rails/view_mocking.rb, line 6 6: def rails_version 7: Rails::VERSION::STRING 8: end
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: elsif rails_version < '2.2' 31: should_render_view_after_version_202(template_name) 32: elsif rails_version < '2.3' 33: should_render_view_22x(template_name) 34: else 35: should_render_view_23x(template_name) 36: end 37: end
This version of should_render_view will work with versions of Rails at Version 2.2.x.
# File lib/flexmock/rails/view_mocking.rb, line 109 109: def should_render_view_22x(template_name) 110: viewmock = flexmock("ViewMock") 111: viewmock.should_receive( 112: :helpers => viewmock, 113: :include => nil, 114: :template_format =>nil, 115: :render => "RENDERED TEXT", 116: :assigns => {}) 117: if template_name 118: viewmock.should_receive(:_exempt_from_layout?).with(/\/#{template_name}$/). 119: and_return(true).once 120: viewmock.should_receive(:_exempt_from_layout?).and_return(true) 121: else 122: viewmock.should_receive(:_exempt_from_layout?).at_least.once.and_return(true) 123: end 124: flexmock(ActionView::Base).should_receive(:new).and_return(viewmock) 125: end
This version of should_render_view will work with versions of Rails at Version 2.3.x.
# File lib/flexmock/rails/view_mocking.rb, line 129 129: def should_render_view_23x(template_name) 130: viewmock = flexmock("ViewMock") 131: viewmock.should_receive( 132: :view_paths => viewmock, 133: :render => "RENDERED TEXT") 134: if template_name 135: viewmock.should_receive(:find_template). 136: with(/\/#{template_name}$/, any). 137: and_return(FlexMock.undefined). 138: at_least.once 139: end 140: viewmock.should_ignore_missing 141: flexmock(ActionView::Base).should_receive(:new).and_return(viewmock) 142: end
This version of should_render_view will work with versions of Rails after Version 1.2.4.
# File lib/flexmock/rails/view_mocking.rb, line 64 64: def should_render_view_after_version_124(template_name) 65: view = flexmock("MockView") 66: view.should_receive( 67: :assigns => {}, 68: :render_file => true, 69: :render_partial => true, 70: :template_format => :dummy_format, 71: :view_paths => :dummy_view_paths, 72: :pick_template_extension => :dummy_extension 73: ) 74: if template_name 75: view.should_receive(:file_exists?).with(/#{template_name}$/).once. 76: and_return(true) 77: end 78: view.should_receive(:file_exists?).with(any).and_return(true) 79: 80: # The number of times this is called changes from version 1.2.6 81: # to 2.0. The important thing is that it is checked at least once. 82: flexmock(@response).should_receive(:template).and_return(view). 83: at_least.once 84: end
This version of should_render_view will work with versions of Rails at Version 2.0.2 and after
# File lib/flexmock/rails/view_mocking.rb, line 88 88: def should_render_view_after_version_202(template_name) 89: viewmock = flexmock("ViewMock") 90: viewmock.should_receive( 91: :assigns => {}, 92: :pick_template_extension => ".html", 93: :template_format =>nil, 94: :view_paths => nil, 95: :file_exists? => true, 96: :first_render => "") 97: if template_name 98: viewmock.should_receive(:render_file).with(/\/#{template_name}$/, any, any). 99: and_return(nil).once 100: viewmock.should_receive(:render_file).and_return(nil) 101: else 102: viewmock.should_receive(:render_file).at_least.once.and_return(nil) 103: end 104: flexmock(ActionView::Base).should_receive(:new).and_return(viewmock) 105: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.