Skip to content

Instantly share code, notes, and snippets.

@ewalk153
Last active December 5, 2022 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ewalk153/ec8b2596fe325e14b90ae777acf0751e to your computer and use it in GitHub Desktop.
Save ewalk153/ec8b2596fe325e14b90ae777acf0751e to your computer and use it in GitHub Desktop.
Demonstrate mocha and minitest behavior with stubs
# ❯ 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