Skip to content

Instantly share code, notes, and snippets.

@rchatley
Created December 18, 2019 13:27
Show Gist options
  • Save rchatley/b30cec1b11ce24b7778f5ea40833f4db to your computer and use it in GitHub Desktop.
Save rchatley/b30cec1b11ce24b7778f5ea40833f4db to your computer and use it in GitHub Desktop.
from unittest.mock import Mock
from order import Order
ROAST_CHICKEN = Order("roast chicken")
APPLE_TART = Order("apple tart")
class HeadChef:
def __init__(self, pastrychef, waiter):
self.waiter = waiter
self.pastrychef = pastrychef
def order(self, main, dessert):
self.pastrychef.order(dessert)
def customer_ready_for(self, dish):
if self.pastrychef.is_cooked(dish):
self.waiter.serve(dish)
class PastryChef:
def order(self, dish):
pass
def is_cooked(self, dish):
pass
class Waiter:
def serve(self, dish):
pass
def test_delegates_dessert_to_pastry_chef():
pastrychef = Mock(spec=PastryChef)
headchef = HeadChef(pastrychef, None)
headchef.order(ROAST_CHICKEN, APPLE_TART)
pastrychef.order.assert_called_once_with(APPLE_TART)
def test_asks_waiter_to_serve_dessert_if_it_is_cooked():
pastrychef = Mock(spec=PastryChef)
waiter = Mock(spec=Waiter)
headchef = HeadChef(pastrychef, waiter)
pastrychef.is_cooked.return_value = True
headchef.customer_ready_for(APPLE_TART)
pastrychef.is_cooked.assert_called_once_with(APPLE_TART)
waiter.serve.assert_called_once_with(APPLE_TART)
def test_does_not_ask_waiter_to_serve_dessert_if_it_is_not_cooked():
pastrychef = Mock(spec=PastryChef)
waiter = Mock(spec=Waiter)
headchef = HeadChef(pastrychef, waiter)
pastrychef.is_cooked.return_value = False
headchef.customer_ready_for(APPLE_TART)
pastrychef.is_cooked.assert_called_once_with(APPLE_TART)
waiter.serve.assert_not_called()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment