Skip to content

Commit

Permalink
- Fixed formatting of unmet mock expectation messages.
Browse files Browse the repository at this point in the history
- Fixed missing must_verify expectation to match assert_mock.
- Fixed assert_mock to fail instead of raise on unmet mock expectations.
- Fixed assert_mock to take an optional message argument.

[git-p4: depot-paths = "//src/minitest/dev/": change = 14353]
  • Loading branch information
zenspider committed Nov 23, 2024
1 parent 212de90 commit 2d542ff
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 10 deletions.
22 changes: 18 additions & 4 deletions lib/minitest/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ def __call name, data # :nodoc:
def verify
@expected_calls.each do |name, expected|
actual = @actual_calls.fetch name, nil # defaults to []
raise MockExpectationError, "expected #{__call name, expected[0]}" unless actual
raise MockExpectationError, "expected #{__call name, expected[actual.size]}, got [#{__call name, actual}]" if
raise MockExpectationError, "Expected #{__call name, expected[0]}" unless actual
raise MockExpectationError, "Expected #{__call name, expected[actual.size]}, got [#{__call name, actual}]" if
actual.size < expected.size
end
true
Expand Down Expand Up @@ -248,13 +248,27 @@ def respond_to? sym, include_private = false # :nodoc:

module Minitest::Assertions
##
# Assert that the mock verifies correctly.
# Assert that the mock verifies correctly and fail if not.

def assert_mock mock
def assert_mock mock, msg = nil
assert mock.verify
rescue MockExpectationError => e
msg = message(msg) { e.message }
flunk msg
end
end

module Minitest::Expectations
##
# See Minitest::Assertions#assert_mock.
#
# _(collection).must_verify
#
# :method: must_verify

infect_an_assertion :assert_mock, :must_verify, :unary
end

##
# Object extensions for Minitest::Mock.

Expand Down
77 changes: 72 additions & 5 deletions test/minitest/test_minitest_mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_allow_return_value_specification
def test_blow_up_if_not_called
@mock.foo

util_verify_bad "expected meaning_of_life() => 42"
util_verify_bad "Expected meaning_of_life() => 42"
end

def test_not_blow_up_if_everything_called
Expand All @@ -46,7 +46,7 @@ def test_not_verify_if_new_expected_method_is_not_called
@mock.meaning_of_life
@mock.expect :bar, true

util_verify_bad "expected bar() => true"
util_verify_bad "Expected bar() => true"
end

def test_blow_up_on_wrong_number_of_arguments
Expand Down Expand Up @@ -184,6 +184,73 @@ def test_mock_is_a_blank_slate
assert @mock == 1, "didn't mock :=="
end

def test_assert_mock__pass
mock = Minitest::Mock.new
mock.expect :loose_expectation, true, [Integer]
mock.loose_expectation 1

result = assert_mock mock

assert_equal true, result
end

def assert_bad_mock klass, msg
mock = Minitest::Mock.new
mock.expect :foo, nil, [:bar]
mock.expect :foo, nil, [:baz]

mock.foo :bar

e = assert_raises klass do
yield mock
end

assert_equal msg, e.message
end

def test_verify__error
exp = "Expected foo(:baz) => nil, got [foo(:bar) => nil]"
assert_bad_mock MockExpectationError, exp do |mock|
mock.verify
end
end

def test_assert_mock__fail
exp = "Expected foo(:baz) => nil, got [foo(:bar) => nil]."
assert_bad_mock Minitest::Assertion, exp do |mock|
assert_mock mock
end
end

def test_assert_mock__fail_msg
exp = "BLAH.\nExpected foo(:baz) => nil, got [foo(:bar) => nil]."
assert_bad_mock Minitest::Assertion, exp do |mock|
assert_mock mock, "BLAH"
end
end

def test_assert_mock__fail_exp
exp = "Expected foo(:baz) => nil, got [foo(:bar) => nil]."
assert_bad_mock Minitest::Assertion, exp do |mock|
describe "X" do
it "y" do
_(mock).must_verify
end
end.new(:blah).send(:test_0001_y)
end
end

def test_assert_mock__fail_exp_msg
exp = "BLAH.\nExpected foo(:baz) => nil, got [foo(:bar) => nil]."
assert_bad_mock Minitest::Assertion, exp do |mock|
describe "X" do
it "y" do
_(mock).must_verify "BLAH"
end
end.new(:blah).send(:test_0001_y)
end
end

def test_verify_allows_called_args_to_be_loosely_specified
mock = Minitest::Mock.new
mock.expect :loose_expectation, true, [Integer]
Expand Down Expand Up @@ -238,7 +305,7 @@ def test_same_method_expects_blow_up_when_not_all_called

e = assert_raises(MockExpectationError) { mock.verify }

exp = "expected foo(:baz) => nil, got [foo(:bar) => nil]"
exp = "Expected foo(:baz) => nil, got [foo(:bar) => nil]"

assert_equal exp, e.message
end
Expand All @@ -252,7 +319,7 @@ def test_same_method_expects_with_same_args_blow_up_when_not_all_called

e = assert_raises(MockExpectationError) { mock.verify }

exp = "expected foo(:bar) => nil, got [foo(:bar) => nil]"
exp = "Expected foo(:bar) => nil, got [foo(:bar) => nil]"

assert_equal exp, e.message
end
Expand All @@ -276,7 +343,7 @@ def test_handles_kwargs_in_error_message

e = assert_raises(MockExpectationError) { mock.verify }

exp = "expected foo(%p) => nil, got [foo(%p) => nil]" \
exp = "Expected foo(%p) => nil, got [foo(%p) => nil]" \
% [{ :kw => false }, { :kw => true }]

assert_equal exp.delete("{}"), e.message
Expand Down
3 changes: 2 additions & 1 deletion test/minitest/test_minitest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,10 @@ def bad_pattern
must_raise
must_respond_to
must_throw
must_verify
path_must_exist]

bad = %w[not raise throw send output be_silent]
bad = %w[not raise throw send output be_silent verify]

expected_wonts = expected_musts.map { |m| m.sub("must", "wont") }.sort
expected_wonts.reject! { |m| m =~ /wont_#{Regexp.union(*bad)}/ }
Expand Down

0 comments on commit 2d542ff

Please sign in to comment.