Created
June 26, 2015 16:30
-
-
Save havenwood/84207afa092ce5d5a680 to your computer and use it in GitHub Desktop.
Mocking a few Monsters with Minitest::Mock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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