-
-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce duplication in & between Mock & ObjectMethods #431
base: main
Are you sure you want to change the base?
Changes from all commits
22c594f
ffac2ad
f700c36
da280b1
7579416
7cff8fa
7494b2d
5f83af5
b9422ba
7a691d9
bf32420
73cf959
b34726e
547a4d5
bf7ef4e
84e3ee7
bfd20d8
9761be3
b2354df
7e7a61b
b87733f
2c4a5db
262ea4c
5a5ed50
5f1c5f4
365884e
19004e7
201cfaa
2840411
583103b
b80cb98
8a7af66
9930e5a
1906a10
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module Mocha | ||
class CompositeExpectation | ||
attr_reader :expectations | ||
|
||
def initialize(expectations) | ||
@expectations = expectations | ||
end | ||
|
||
%w[ | ||
times twice once never at_least at_least_once at_most at_most_once | ||
with yields multiple_yields returns raises throws then when in_sequence | ||
].each do |method_name| | ||
define_method(method_name) do |*args, &block| | ||
CompositeExpectation.new(@expectations.map { |e| e.send(method_name, *args, &block) }) | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
require 'mocha/singleton_class' | ||
require 'mocha/expectation' | ||
require 'mocha/expectation_list' | ||
require 'mocha/composite_expectation' | ||
require 'mocha/invocation' | ||
require 'mocha/names' | ||
require 'mocha/receivers' | ||
require 'mocha/method_matcher' | ||
require 'mocha/parameters_matcher' | ||
require 'mocha/argument_iterator' | ||
require 'mocha/expectation_error_factory' | ||
require 'mocha/ruby_version' | ||
|
||
|
@@ -109,14 +109,15 @@ class Mock | |
# object.expects(:expected_method_one).returns(:result_one) | ||
# object.expects(:expected_method_two).returns(:result_two) | ||
def expects(method_name_or_hash, backtrace = nil) | ||
iterator = ArgumentIterator.new(method_name_or_hash) | ||
iterator.each do |*args| | ||
CompositeExpectation.new(Array(method_name_or_hash).map do |*args| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the documentation:
This introduces a breaking change. So, even if we agree that the new behavior is preferable, we'd have to think about deprecating the old behavior. There are no existing tests to check for the documented behavior, though. |
||
args = args.flatten | ||
method_name = args.shift | ||
yield method_name if block_given? | ||
ensure_method_not_already_defined(method_name) | ||
expectation = Expectation.new(self, method_name, backtrace) | ||
expectation.returns(args.shift) unless args.empty? | ||
@expectations.add(expectation) | ||
end | ||
end) | ||
end | ||
|
||
# Adds an expectation that the specified method may be called any number of times with any parameters. | ||
|
@@ -145,15 +146,7 @@ def expects(method_name_or_hash, backtrace = nil) | |
# object.stubs(:stubbed_method_one).returns(:result_one) | ||
# object.stubs(:stubbed_method_two).returns(:result_two) | ||
def stubs(method_name_or_hash, backtrace = nil) | ||
iterator = ArgumentIterator.new(method_name_or_hash) | ||
iterator.each do |*args| | ||
method_name = args.shift | ||
ensure_method_not_already_defined(method_name) | ||
expectation = Expectation.new(self, method_name, backtrace) | ||
expectation.at_least(0) | ||
expectation.returns(args.shift) unless args.empty? | ||
@expectations.add(expectation) | ||
end | ||
expects(method_name_or_hash, backtrace).at_least(0) | ||
end | ||
|
||
# Removes the specified stubbed methods (added by calls to {#expects} or {#stubs}) and all expectations associated with them. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remember to add with_block_given and with_no_block_given when merging