Last active
December 5, 2022 21:32
-
-
Save ewalk153/ec8b2596fe325e14b90ae777acf0751e to your computer and use it in GitHub Desktop.
Demonstrate mocha and minitest behavior with stubs
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
# ❯ ruby foo_test.rb | |
# Run options: --seed 44288 | |
# # Running: | |
# F. | |
# Finished in 0.000679s, 2945.5069 runs/s, 2945.5069 assertions/s. | |
# 1) Failure: | |
# StubbyStubTest#test_foo_with_a_mock [foo.rb:20]: | |
# unexpected invocation: #<Foo:0x370>.foo(:b) | |
# satisfied expectations: | |
# - allowed any number of times, invoked once: #<Foo:0x370>.foo(:a) | |
# 2 runs, 2 assertions, 1 failures, 0 errors, 0 skips | |
require 'minitest/autorun' | |
require 'mocha/minitest' | |
class Foo | |
def foo(param) | |
"param: #{param}" | |
end | |
end | |
class StubbyStubTest < Minitest::Test | |
def test_foo_with_a | |
assert_equal "param: a", Foo.new.foo(:a) | |
end | |
def test_foo_with_a_mock | |
foo = Foo.new | |
foo.stubs(:foo).with(:a).returns("test") | |
assert_equal "test", foo.foo(:a) | |
assert_equal "param: b", foo.foo(:b) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment