Class: FlexMock::MockContainerHelper
- Inherits:
-
Object
- Object
- FlexMock::MockContainerHelper
- Includes:
- ArgumentTypes
- Defined in:
- lib/flexmock/mock_container.rb
Overview
################################################################# Helper methods for mock containers. MockContainer is a module that is designed to be mixed into other classes, particularly testing framework test cases. Since we don’t want to pollute the method namespace of the class that mixes in MockContainer, a number of MockContainer methods were moved into ContainerHelper to to isoloate the names.
Instance Method Summary collapse
-
#add_model_methods(mock, model_class, id) ⇒ Object
Automatically add mocks for some common methods in ActiveRecord models.
-
#make_partial_proxy(container, obj, name, safe_mode) ⇒ Object
Create a PartialMockProxy for the given object.
-
#next_id ⇒ Object
Return the next id for mocked models.
-
#parse_should_args(mock, args, &block) ⇒ Object
:call-seq: parse_should_args(args) { |symbol| … }.
Methods included from ArgumentTypes
#any, #ducktype, #eq, #hsh, #on
Instance Method Details
#add_model_methods(mock, model_class, id) ⇒ Object
Automatically add mocks for some common methods in ActiveRecord models.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/flexmock/mock_container.rb', line 212 def add_model_methods(mock, model_class, id) container = mock.flexmock_container mock_errors = container.flexmock("errors") mock_errors.should_receive(:count).and_return(0).by_default mock_errors.should_receive(:full_messages).and_return([]).by_default mock.should_receive(:id).and_return(id).by_default mock.should_receive(:to_params).and_return(id.to_s).by_default mock.should_receive(:new_record?).and_return(false).by_default mock.should_receive(:class).and_return(model_class).by_default mock.should_receive(:errors).and_return(mock_errors).by_default # HACK: Ruby 1.9 needs the following lambda so that model_class # is correctly bound below. lambda { } mock.should_receive(:is_a?).with(any).and_return { |other| other == model_class }.by_default mock.should_receive(:instance_of?).with(any).and_return { |other| other == model_class }.by_default mock.should_receive(:kind_of?).with(any).and_return { |other| model_class.ancestors.include?(other) }.by_default end |
#make_partial_proxy(container, obj, name, safe_mode) ⇒ Object
Create a PartialMockProxy for the given object. Use name
as the name of the mock object.
241 242 243 244 245 246 247 248 249 |
# File 'lib/flexmock/mock_container.rb', line 241 def make_partial_proxy(container, obj, name, safe_mode) name ||= "flexmock(#{obj.class.to_s})" if !obj.instance_variable_defined?("@flexmock_proxy") || obj.instance_variable_get("@flexmock_proxy").nil? mock = FlexMock.new(name, container) proxy = PartialMockProxy.new(obj, mock, safe_mode) obj.instance_variable_set("@flexmock_proxy", proxy) end obj.instance_variable_get("@flexmock_proxy") end |
#next_id ⇒ Object
Return the next id for mocked models.
181 182 183 184 |
# File 'lib/flexmock/mock_container.rb', line 181 def next_id @id_counter ||= 10000 @id_counter += 1 end |
#parse_should_args(mock, args, &block) ⇒ Object
:call-seq:
parse_should_args(args) { |symbol| ... }
This method provides common handling for the various should_receive argument lists. It sorts out the differences between symbols, arrays and hashes, and identifies the method names specified by each. As each method name is identified, create a mock expectation for it using the supplied block.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/flexmock/mock_container.rb', line 194 def parse_should_args(mock, args, &block) # :nodoc: result = CompositeExpectation.new args.each do |arg| case arg when Hash arg.each do |k,v| exp = build_demeter_chain(mock, k, &block).and_return(v) result.add(exp) end when Symbol, String result.add(build_demeter_chain(mock, arg, &block)) end end result end |