Skip to content

Instantly share code, notes, and snippets.

@6ewis
Forked from havenwood/mock_monster.rb
Created June 17, 2016 20:48
Show Gist options
  • Save 6ewis/99f4ae293257370a220f02f21d75e088 to your computer and use it in GitHub Desktop.
Save 6ewis/99f4ae293257370a220f02f21d75e088 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