Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mocking a few Monsters with Minitest::Mock
require 'minitest/mock'
##
# This monster eats Symbols. Give it a Symbol and how many to eat and nom nom!
monster = Minitest::Mock.new
monster.expect :eat, :nom_nom, [Symbol, Numeric]
##
# Let's verify the monster in fact ate its Symbols:
monster.verify
#!> MockExpectationError: expected eat(Symbol, Numeric) => :nom_nom
##
# Nope, it hasn't. So let's feed it a String:
monster.eat 'village', 5
#!> MockExpectationError: mocked method :eat called with unexpected arguments ["village", 5]
##
# This monster only eats Symbols:
monster.eat :village, 5
#=> :nom_nom
monster.verify
#=> true
##
# C is for Cookie!
cookie_monster = Minitest::Mock.new
cookie_monster.expect :eat, :om_nom_nom_nom, [:cookie, Numeric]
##
# Cookie Monster doesn't eat villages...
cookie_monster.eat :village, 5
#!> MockExpectationError: mocked method :eat called with unexpected arguments [:village, 5]
cookie_monster.eat :cookie, 5
#=> :om_nom_nom_nom
cookie_monster.verify
#=> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.