-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more consistent setting of multiple expectations
When calling expects/stubs with a hash (method_names_vs_return_values), only the last expectation would get returned. Any further 'expectation setting' methods chained to that call would, therefore, get called only on the last expectation. This seems arbitrary, and neither evident nor intuitive. We now 'extract' the expectation setting methods into a _virtual_ interface, 'implemented' by both Expectation and ExpectationSetting, and return ExpectationSetting instead of Expectation from the expects/stubs methods. This allows us to pass any further expectation setting method calls on to _each_ of the multiple expectations, rather than just the _last_ (or some other arbitrary) single expectation.
- Loading branch information
Showing
5 changed files
with
64 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Mocha | ||
class ExpectationSetting | ||
EXPECTATION_SETTING_METHODS = %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 | ||
].map(&:to_sym).freeze | ||
|
||
attr_reader :expectations | ||
|
||
def initialize(expectations) | ||
@expectations = expectations | ||
end | ||
|
||
def respond_to_missing?(symbol, _include_all = false) | ||
EXPECTATION_SETTING_METHODS.include?(symbol) || super | ||
end | ||
|
||
def method_missing(symbol, *arguments, &block) | ||
if EXPECTATION_SETTING_METHODS.include?(symbol) | ||
ExpectationSetting.new(@expectations.map { |e| e.send(symbol, *arguments, &block) }) | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters