-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display matching invocations alongside expectations
First bunch of commits from #394. Co-authored-by: Nitish Rathi <[email protected]>
- Loading branch information
Showing
5 changed files
with
140 additions
and
16 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
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,31 @@ | ||
require 'mocha/parameters_matcher' | ||
require 'mocha/raised_exception' | ||
|
||
module Mocha | ||
class Invocation | ||
def initialize(method_name, yield_parameters, return_values) | ||
@method_name = method_name | ||
@yield_parameters = yield_parameters | ||
@return_values = return_values | ||
@yields = [] | ||
end | ||
|
||
def call(*arguments) | ||
@arguments = ParametersMatcher.new(arguments) | ||
@yield_parameters.next_invocation.each do |yield_parameters| | ||
@yields << ParametersMatcher.new(yield_parameters) | ||
yield(*yield_parameters) | ||
end | ||
@result = @return_values.next | ||
rescue Exception => e # rubocop:disable Lint/RescueException | ||
@result = RaisedException.new(e) | ||
raise | ||
end | ||
|
||
def mocha_inspect | ||
desc = "\n - #{@method_name}#{@arguments.mocha_inspect} # => #{@result.mocha_inspect}" | ||
desc << " after yielding #{@yields.map(&:mocha_inspect).join(', then ')}" if @yields.any? | ||
desc | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Mocha | ||
class RaisedException | ||
def initialize(exception) | ||
@exception = exception | ||
end | ||
|
||
def mocha_inspect | ||
"raised #{@exception}" | ||
end | ||
end | ||
end |
72 changes: 72 additions & 0 deletions
72
test/acceptance/display_matching_invocations_alongside_expectations_test.rb
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,72 @@ | ||
require File.expand_path('../acceptance_test_helper', __FILE__) | ||
require 'mocha/setup' | ||
|
||
class DisplayMatchingInvocationsAlongsideExpectationsTest < Mocha::TestCase | ||
include AcceptanceTest | ||
|
||
def setup | ||
setup_acceptance_test | ||
@original_env = ENV.to_hash | ||
ENV['MOCHA_OPTIONS'] = 'verbose' | ||
end | ||
|
||
def teardown | ||
ENV.replace(@original_env) | ||
teardown_acceptance_test | ||
end | ||
|
||
def test_should_display_results | ||
test_result = run_as_test do | ||
foo = mock('foo') | ||
foo.expects(:bar).with(1).returns('a') | ||
foo.stubs(:bar).with(any_parameters).returns('f').raises(StandardError) | ||
|
||
foo.bar(1, 2) | ||
assert_raise(StandardError) { foo.bar(3, 4) } | ||
end | ||
assert_invocations( | ||
test_result, | ||
'- allowed any number of times, invoked twice: #<Mock:foo>.bar(any_parameters)', | ||
' - #<Mock:foo>.bar(1, 2) # => "f"', | ||
' - #<Mock:foo>.bar(3, 4) # => raised StandardError' | ||
) | ||
end | ||
|
||
def test_should_display_yields | ||
test_result = run_as_test do | ||
foo = mock('foo') | ||
foo.expects(:bar).with(1).returns('a') | ||
foo.stubs(:bar).with(any_parameters).multiple_yields(%w[b c], %w[d e]).returns('f').raises(StandardError) | ||
|
||
foo.bar(1, 2) { |_ignored| } | ||
assert_raise(StandardError) { foo.bar(3, 4) { |_ignored| } } | ||
end | ||
assert_invocations( | ||
test_result, | ||
'- allowed any number of times, invoked twice: #<Mock:foo>.bar(any_parameters)', | ||
' - #<Mock:foo>.bar(1, 2) # => "f" after yielding ("b", "c"), then ("d", "e")', | ||
' - #<Mock:foo>.bar(3, 4) # => raised StandardError after yielding ("b", "c"), then ("d", "e")' | ||
) | ||
end | ||
|
||
def test_should_display_empty_yield_and_return | ||
test_result = run_as_test do | ||
foo = mock('foo') | ||
foo.expects(:bar).with(1).returns('a') | ||
foo.stubs(:bar).with(any_parameters).yields | ||
|
||
foo.bar(1, 2) { |_ignored| } | ||
end | ||
assert_invocations( | ||
test_result, | ||
'- allowed any number of times, invoked once: #<Mock:foo>.bar(any_parameters)', | ||
' - #<Mock:foo>.bar(1, 2) # => nil after yielding ()' | ||
) | ||
end | ||
|
||
def assert_invocations(test_result, *invocations) | ||
assert_failed(test_result) | ||
assert_equal invocations.unshift('satisfied expectations:'), | ||
test_result.failure_message_lines[-invocations.size..-1] | ||
end | ||
end |