Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created June 26, 2015 16:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save havenwood/84207afa092ce5d5a680 to your computer and use it in GitHub Desktop.
Save havenwood/84207afa092ce5d5a680 to your computer and use it in GitHub Desktop.
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