Class: FlexMock
- Extended by:
- ArgumentTypes
- Includes:
- Ordering
- Defined in:
- lib/flexmock/core.rb,
lib/flexmock/rspec.rb,
lib/flexmock/errors.rb,
lib/flexmock/version.rb,
lib/flexmock/ordering.rb,
lib/flexmock/recorder.rb,
lib/flexmock/undefined.rb,
lib/flexmock/validators.rb,
lib/flexmock/call_record.rb,
lib/flexmock/expectation.rb,
lib/flexmock/mock_builder.rb,
lib/flexmock/partial_mock.rb,
lib/flexmock/argument_types.rb,
lib/flexmock/call_validator.rb,
lib/flexmock/mock_container.rb,
lib/flexmock/spy_describers.rb,
lib/flexmock/explicit_needed.rb,
lib/flexmock/argument_matchers.rb,
lib/flexmock/argument_matching.rb,
lib/flexmock/rspec_spy_matcher.rb,
lib/flexmock/core_class_methods.rb,
lib/flexmock/deprecated_methods.rb,
lib/flexmock/expectation_builder.rb,
lib/flexmock/expectation_director.rb,
lib/flexmock/expectation_recorder.rb,
lib/flexmock/minitest_integration.rb,
lib/flexmock/composite_expectation.rb,
lib/flexmock/test_unit_integration.rb,
lib/flexmock/default_framework_adapter.rb,
lib/flexmock/test_unit_assert_spy_called.rb,
lib/flexmock/test_unit_testcase_extensions.rb,
lib/flexmock/extensions/active_record_model.rb more...
Overview
Permission is granted for use, copying, modification, distribution, and distribution of modified versions of this work as long as the above copyright notice is included. +++
Defined Under Namespace
Modules: ArgumentMatching, ArgumentTypes, Extensions, GenericTestCase, Minitest, MockContainer, Ordering, RSpecMatchers, SpyDescribers, TestCase, TestUnitAssertions Classes: AnyMatcher, AtLeastCountValidator, AtMostCountValidator, CallRecord, CallValidator, CheckFailedError, CompositeExpectation, CountValidator, DefaultFrameworkAdapter, DuckMatcher, EqualMatcher, ExactCountValidator, Expectation, ExpectationBuilder, ExpectationDirector, ExpectationRecorder, ExplicitNeeded, ExtensionRegistry, HashMatcher, MinitestFrameworkAdapter, MockBuilder, MockError, NullParentMock, PartialMockProxy, ProcMatcher, RSpecFrameworkAdapter, Recorder, SignatureValidator, TestUnitFrameworkAdapter, Undefined, UsageError, UseContainer
Constant Summary collapse
- CALL_VALIDATOR =
CallValidator.new
- ON_RUBY_20 =
(RUBY_VERSION =~ /^2\.0\./)
- SpecModule =
Spec
- VERSION =
"3.0.1"
- CONTAINER_HELPER =
ExtensionRegistry.new
- ANY =
AnyMatcher.new
- FORBID_MOCKING =
:__flexmock_forbid_mocking
- EXP_BUILDER =
ExpectationBuilder.new
Class Attribute Summary collapse
-
.framework_adapter ⇒ Object
readonly
Returns the value of attribute framework_adapter.
-
.partials_are_based ⇒ Object
Returns the value of attribute partials_are_based.
-
.partials_verify_signatures ⇒ Object
Returns the value of attribute partials_verify_signatures.
Instance Attribute Summary collapse
-
#flexmock_container_stack ⇒ Object
readonly
Returns the value of attribute flexmock_container_stack.
-
#flexmock_name ⇒ Object
readonly
Returns the value of attribute flexmock_name.
Class Method Summary collapse
-
.check(msg, &block) ⇒ Object
Check will assert the block returns true.
-
.forbid_mocking(mocking_forbidden_return = nil) ⇒ Object
Forbid mock calls to happen while the block is being evaluated.
-
.format_args(args, kw) ⇒ Object
Class method to format a list of args (the part between the parenthesis).
-
.format_call(sym, args, kw) ⇒ Object
Class method to format a method name and argument list as a nice looking string.
-
.undefined ⇒ Object
Undefined is normally available as FlexMock.undefined.
-
.use(*names) ⇒ Object
Class method to make sure that verify is called at the end of a test.
-
.verify_mocking_allowed! ⇒ Object
Verify that mocking is allowed in the current context.
Instance Method Summary collapse
- #by_default ⇒ Object
- #flexmock_base_class ⇒ Object
- #flexmock_based_on(base_class) ⇒ Object
-
#flexmock_calls ⇒ Object
Return the list of calls made on this mock.
- #flexmock_closed? ⇒ Boolean
- #flexmock_container ⇒ Object
-
#flexmock_define_expectation(location, *args, **kw) ⇒ Object
Using
location
, define the expectations specified byargs
. -
#flexmock_expectations_for(method_name) ⇒ Object
Return the expectation director for a method name.
-
#flexmock_find_expectation(method_name, *args, **kw, &block) ⇒ Object
Find the mock expectation for method sym and arguments.
-
#flexmock_invoke_original(method_name, args, kw = {}, orig_block = nil) ⇒ Object
Invocke the original non-mocked functionality for the given symbol.
-
#flexmock_received?(method_name, args, kw, options = {}) ⇒ Boolean
True if the mock received the given method and arguments.
-
#flexmock_respond_to? ⇒ Object
Save the original definition of respond_to? for use a bit later.
-
#flexmock_teardown ⇒ Object
Teardown and infrastructure setup for this mock.
-
#flexmock_verify ⇒ Object
Verify that each method that had an explicit expected count was actually called that many times.
- #ignore_missing? ⇒ Boolean
-
#initialize(name = "unknown", container = nil, parent: nil) ⇒ FlexMock
constructor
Create a FlexMock object with the given name.
-
#inspect ⇒ Object
Return the inspection string for a mock.
-
#method(method_name) ⇒ Object
Override the built-in
method
to include the mocked methods. -
#method_missing(sym, *args, **kw, &block) ⇒ Object
Handle missing methods by attempting to look up a handler.
-
#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. - #pop_flexmock_container ⇒ Object
- #push_flexmock_container(container) ⇒ Object
-
#respond_to?(sym, *args) ⇒ Boolean
Override the built-in respond_to? to include the mocked methods.
-
#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.
-
#should_ignore_missing ⇒ Object
(also: #mock_ignore_missing)
Ignore all undefined (missing) method calls.
-
#should_receive(*args, **kw) ⇒ Object
:call-seq: mock.should_receive(:method_name) mock.should_receive(:method1, method2, …) mock.should_receive(:meth1 => result1, :meth2 => result2, …).
Methods included from ArgumentTypes
Methods included from Ordering
#flexmock_allocate_order, #flexmock_current_order, #flexmock_current_order=, #flexmock_groups, #flexmock_validate_order
Constructor Details
permalink #initialize(name = "unknown", container = nil, parent: 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.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/flexmock/core.rb', line 66 def initialize(name="unknown", container=nil, parent: nil) @flexmock_name = name @flexmock_closed = false @flexmock_container_stack = Array.new @expectations = Hash.new @verified = false @calls = [] @base_class = nil if parent @ignore_missing = parent.ignore_missing? @parent_mock = parent else @ignore_missing = false @parent_mock = NullParentMock.new end container = UseContainer.new if container.nil? container.flexmock_remember(self) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
permalink #method_missing(sym, *args, **kw, &block) ⇒ Object
Handle missing methods by attempting to look up a handler.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/flexmock/core.rb', line 140 def method_missing(sym, *args, **kw, &block) FlexMock.verify_mocking_allowed! call_record = CallRecord.new(sym, args, kw, block) @calls << call_record flexmock_wrap do if flexmock_closed? FlexMock.undefined elsif exp = flexmock_expectations_for(sym) exp.call(args, kw, block, call_record) elsif @base_class && @base_class.flexmock_defined?(sym) FlexMock.undefined elsif @ignore_missing FlexMock.undefined else super(sym, *args, **kw, &block) end end end |
Class Attribute Details
permalink .framework_adapter ⇒ Object (readonly)
Returns the value of attribute framework_adapter.
17 18 19 |
# File 'lib/flexmock/core_class_methods.rb', line 17 def framework_adapter @framework_adapter end |
Instance Attribute Details
Class Method Details
permalink .check(msg, &block) ⇒ Object
Check will assert the block returns true. If it doesn’t, an assertion failure is triggered with the given message.
121 122 123 124 125 126 127 |
# File 'lib/flexmock/core_class_methods.rb', line 121 def check(msg, &block) # :nodoc: if FlexMock.framework_adapter.respond_to?(:check) FlexMock.framework_adapter.check(msg, &block) else FlexMock.framework_adapter.make_assertion(msg, &block) end end |
permalink .forbid_mocking(mocking_forbidden_return = nil) ⇒ Object
Forbid mock calls to happen while the block is being evaluated
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/flexmock/core_class_methods.rb', line 58 def forbid_mocking(mocking_forbidden_return = nil) current, Thread.current[FORBID_MOCKING] = Thread.current[FORBID_MOCKING], true catch(FORBID_MOCKING) do return yield end mocking_forbidden_return ensure Thread.current[FORBID_MOCKING] = current end |
permalink .format_args(args, kw) ⇒ Object
Class method to format a list of args (the part between the parenthesis).
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/flexmock/core_class_methods.rb', line 87 def format_args(args, kw) args = if args args = args.map do |a| FlexMock.forbid_mocking("<recursive call to mocked method in #inspect>") do a.inspect end end args.join(', ') else "*args" end kw = if kw.kind_of? HashMatcher kw.inspect elsif kw && !kw.empty? kw = kw.transform_values do |v| FlexMock.forbid_mocking("<recursive call to mocked method in #inspect>") do v.inspect end end kw.map { |k, v| "#{k}: #{v}" }.join(', ') elsif kw.nil? # Don't append **kwargs to signature if ruby version < 3 # in order to not break existing code still on ruby2 "**kwargs" unless RUBY_VERSION < "3" end [(args unless args.empty?), kw].compact.join(", ") end |
permalink .format_call(sym, args, kw) ⇒ Object
Class method to format a method name and argument list as a nice looking string.
81 82 83 |
# File 'lib/flexmock/core_class_methods.rb', line 81 def format_call(sym, args, kw) # :nodoc: "#{sym}(#{format_args(args, kw)})" end |
permalink .undefined ⇒ Object
Undefined is normally available as FlexMock.undefined
47 48 49 |
# File 'lib/flexmock/undefined.rb', line 47 def self.undefined @undefined end |
permalink .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/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 => _ container.got_exception = true raise ensure container.flexmock_teardown end |
permalink .verify_mocking_allowed! ⇒ Object
Verify that mocking is allowed in the current context. Throws if it is not.
73 74 75 76 77 |
# File 'lib/flexmock/core_class_methods.rb', line 73 def verify_mocking_allowed! if Thread.current[FORBID_MOCKING] throw FORBID_MOCKING end end |
Instance Method Details
permalink #by_default ⇒ Object
[View source]
134 135 136 137 |
# File 'lib/flexmock/core.rb', line 134 def by_default @last_expectation.by_default self end |
permalink #flexmock_base_class ⇒ Object
[View source]
180 181 182 |
# File 'lib/flexmock/core.rb', line 180 def flexmock_base_class @base_class end |
permalink #flexmock_based_on(base_class) ⇒ Object
[View source]
184 185 186 187 188 189 190 191 192 |
# File 'lib/flexmock/core.rb', line 184 def flexmock_based_on(base_class) @base_class = base_class if base_class <= Kernel if self.class != base_class should_receive(:class => base_class) should_receive(:kind_of?).and_return { |against| base_class <= against } end end end |
permalink #flexmock_calls ⇒ Object
Return the list of calls made on this mock. Used in formatting error messages.
203 204 205 |
# File 'lib/flexmock/core.rb', line 203 def flexmock_calls @calls end |
permalink #flexmock_closed? ⇒ Boolean
123 124 125 |
# File 'lib/flexmock/core.rb', line 123 def flexmock_closed? @flexmock_closed end |
permalink #flexmock_container ⇒ Object
[View source]
89 90 91 |
# File 'lib/flexmock/core.rb', line 89 def flexmock_container flexmock_container_stack.last end |
permalink #flexmock_define_expectation(location, *args, **kw) ⇒ Object
Using location
, define the expectations specified by args
.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/flexmock/core.rb', line 254 def flexmock_define_expectation(location, *args, **kw) @last_expectation = EXP_BUILDER.parse_should_args(self, args, kw) do |method_name| exp = flexmock_expectations_for(method_name) || ExpectationDirector.new(method_name) @expectations[method_name] = exp result = Expectation.new(self, method_name, location) exp << result override_existing_method(method_name) if flexmock_respond_to?(method_name, true) if @base_class && !@base_class.flexmock_defined?(method_name) if !ON_RUBY_20 || !@base_class.ancestors.include?(Class) result = ExplicitNeeded.new(result, method_name, @base_class) end end result end end |
permalink #flexmock_expectations_for(method_name) ⇒ Object
Return the expectation director for a method name.
176 177 178 |
# File 'lib/flexmock/core.rb', line 176 def flexmock_expectations_for(method_name) # :nodoc: @expectations[method_name] || @parent_mock.flexmock_expectations_for(method_name) end |
permalink #flexmock_find_expectation(method_name, *args, **kw, &block) ⇒ Object
Find the mock expectation for method sym and arguments.
169 170 171 172 173 |
# File 'lib/flexmock/core.rb', line 169 def flexmock_find_expectation(method_name, *args, **kw, &block) # :nodoc: if exp = flexmock_expectations_for(method_name) exp.find_expectation(args, kw, block) end end |
permalink #flexmock_invoke_original(method_name, args, kw = {}, orig_block = nil) ⇒ Object
Invocke the original non-mocked functionality for the given symbol.
209 210 211 |
# File 'lib/flexmock/core.rb', line 209 def flexmock_invoke_original(method_name, args, kw = {}, orig_block = nil) return FlexMock.undefined end |
permalink #flexmock_received?(method_name, args, kw, options = {}) ⇒ Boolean
True if the mock received the given method and arguments.
197 198 199 |
# File 'lib/flexmock/core.rb', line 197 def flexmock_received?(method_name, args, kw, = {}) CALL_VALIDATOR.received?(@calls, method_name, args, kw, ) end |
permalink #flexmock_respond_to? ⇒ Object
Save the original definition of respond_to? for use a bit later.
161 |
# File 'lib/flexmock/core.rb', line 161 alias flexmock_respond_to? respond_to? |
permalink #flexmock_teardown ⇒ Object
Teardown and infrastructure setup for this mock.
119 120 121 |
# File 'lib/flexmock/core.rb', line 119 def flexmock_teardown @flexmock_closed = true end |
permalink #flexmock_verify ⇒ Object
Verify that each method that had an explicit expected count was actually called that many times.
108 109 110 111 112 113 114 115 116 |
# File 'lib/flexmock/core.rb', line 108 def flexmock_verify return if @verified @verified = true flexmock_wrap do @expectations.each do |sym, handler| handler.flexmock_verify end end end |
permalink #ignore_missing? ⇒ Boolean
85 86 87 |
# File 'lib/flexmock/core.rb', line 85 def ignore_missing? @ignore_missing end |
permalink #inspect ⇒ Object
Return the inspection string for a mock.
102 103 104 |
# File 'lib/flexmock/core.rb', line 102 def inspect "<FlexMock:#{flexmock_name}>" end |
permalink #method(method_name) ⇒ Object
Override the built-in method
to include the mocked methods.
214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/flexmock/core.rb', line 214 def method(method_name) if (expectations = flexmock_expectations_for(method_name)) ->(*args, **kw, &block) { expectations.call(args, kw, block) } else super end rescue NameError => ex if ignore_missing? proc { FlexMock.undefined } else raise ex end end |
permalink #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 44 |
# File '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 |
permalink #pop_flexmock_container ⇒ Object
[View source]
97 98 99 |
# File 'lib/flexmock/core.rb', line 97 def pop_flexmock_container flexmock_container_stack.pop end |
permalink #push_flexmock_container(container) ⇒ Object
[View source]
93 94 95 |
# File 'lib/flexmock/core.rb', line 93 def push_flexmock_container(container) flexmock_container_stack.push(container) end |
permalink #respond_to?(sym, *args) ⇒ Boolean
Override the built-in respond_to? to include the mocked methods.
164 165 166 |
# File 'lib/flexmock/core.rb', line 164 def respond_to?(sym, *args) super || (@expectations[sym] ? true : @ignore_missing) end |
permalink #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
283 284 285 |
# File 'lib/flexmock/core.rb', line 283 def should_expect yield Recorder.new(self) end |
permalink #should_ignore_missing ⇒ Object Also known as: mock_ignore_missing
Ignore all undefined (missing) method calls.
128 129 130 131 |
# File 'lib/flexmock/core.rb', line 128 def should_ignore_missing @ignore_missing = true self end |
permalink #should_receive(*args, **kw) ⇒ 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.
247 248 249 |
# File 'lib/flexmock/core.rb', line 247 def should_receive(*args, **kw) flexmock_define_expectation(caller, *args, **kw) end |