Let's put up this scenario
# This code lives in lolcat-web/lolapps/lolburger/burger.py
import os
from helper.lib import price
def i_haz_cheezeburger(Burger, user):
kings_burgers = []
queens_burgers = []
address = os.path.join(user.hometown, burger_king_addr)
burger_menu = Burger.menu()
for burger in burger_menu:
if burger == 'King':
kings_burgers.append({
'name': burger,
'price': price(burger)
})
else:
queens_burgers.append(...)
return (kings_burgers, queens_burgers)
I want to test whether the function i_haz_cheezeburger
will return a tuple, with correct values in it. It depends on a few things (simplification): os
, helper.lib.price
, Burger
, and user
(User class)
Since I am fairly new to mock
by Michael Foord, here is my newbie attempt:
edit looks wrong because joiner
and price
doesn't seem to be used anywhere?
def test_01(unittest.TestCase):
with patch('os.path.join') as joiner:
with patch('lolcat-web.lolapps.lolburger.burger.price') as price:
mock_user = MagicMock(name='user', spec=client.auth.User)
mock_user.hometown = 'New-York'
mock_burger = MagicMock(name='burger', spec=lolcat-web.lolapps.lolburger.Burger)
mock_burger.menu = [ '....sets up a list of samples .....' ]
# now call the function
response = i_get_cheezeburger(mock_burger, mock_user)
# now we do some assertions.....
This is like writing fixtures. What if the method i_haz_cheezeburger
has a few more dependencies. Of course I can refactor them out and create a few more wrapper / helper functions. But the problem is, or at least the confusion is:
I want to test my output. I don't want to know "oh there is no mispell / unbound assignment because it passed with a per-defined return value" But setting up the fixtures (or return values for things like Burger.menu
) doesn't sound right.
What's the proper way of dealing this kind of issue? Please correct my misunderstanding.