Skip to content

Instantly share code, notes, and snippets.

@JonathonMA
Created September 17, 2013 04:30
Show Gist options
  • Save JonathonMA/6590083 to your computer and use it in GitHub Desktop.
Save JonathonMA/6590083 to your computer and use it in GitHub Desktop.
F@cking Mocks? How do they work?
# Note minitest 2 doesn't run out of assertions so that test case would fail
gem 'minitest', '~> 4'
require 'minitest/unit'
require 'minitest/mock'
require 'minitest/autorun'
def do_nothing_with object
end
def do_something_with object
object.do_something!
end
class WatMockTest < MiniTest::Unit::TestCase
def test_if_this_test_fails_you_dont_assert_verify
mock = MiniTest::Mock.new
mock.expect :foo, true
do_nothing_with mock
assert_raises MockExpectationError do
mock.verify
end
end
def test_also_mocks_fails_if_you_send_them_unexpected_methods
mock = MiniTest::Mock
#mock.expect(:do_something).never
assert_raises NoMethodError do
do_something_with mock
end
end
def test_you_can_use_object_new_for_dont_sent_mocks
mock = Object.new
assert_raises NoMethodError do
do_something_with mock
end
end
def test_yeah_you_dont_need_to_verify_you_run_out_of_assertions
mock = MiniTest::Mock.new
mock.expect :do_something!, true
do_something_with mock
assert_raises MockExpectationError do
do_something_with mock
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment