How would I stub the output of pickCard()
function which is being called twice in deal()
function? I want to test both losing and winning cases.
For example, I would like to have for the winning case, the first time pickCard()
is called is given value 8
to card1
, and second given value 10
to card2
.
I have tried using @Mock.patch, but this works only for doing one call.
I have used self.blackjack.pickCard = MagicMock(return_value=8)
but again if i use it twice it will overwrite the return value
Here is the code:
import random
class Game:
def __init__(self):
self.cards = [1,2,3,4,5,6,7,8,9,10]
def deal(self):
card1 = self.pickCard()
self.removeCards(card1)
card2 = self.pickCard()
return card1 + card2 > 16
def pickCard(self):
return random.choice(self.cards)
def removeCards(self,card1):
return self.cards.remove(card1)
The test file is:
import unittest
from mock import MagicMock
import mock
from lib.game import Game
class TestGame(unittest.TestCase):
def setUp(self):
self.game = Game()
def test_0(self):#passing
"""Only cards from 1 to 10 exist"""
self.assertListEqual(self.game.cards, [1,2,3,4,5,6,7,8,9,10])
#Here is where I am finding difficulty writing the test
def test_1(self):
"""Player dealt winning card"""
with mock.patch('lib.game.Game.pickCard') as mock_pick:
mock_pick.side_effect = (8, 10)
g = Game()
g.pickCard()
g.pickCard()
self.assertTrue(self.game.deal())
EDIT
I ran this test with above code, and I get this stack trace instead of passing
Traceback (most recent call last):
tests/game_test.py line 26 in test_1
self.assertTrue(self.game.deal())
lib/game.py line 8 in deal
card1 = self.pickCard()
/usr/local/lib/python2.7/site-packages/mock/mock.py line 1062 in __call__
return _mock_self._mock_call(*args, **kwargs)
/usr/local/lib/python2.7/site-packages/mock/mock.py line 1121 in _mock_call
result = next(effect)
/usr/local/lib/python2.7/site-packages/mock/mock.py line 109 in next
return _next(obj)
Do I need to put the two g.pickCard()
elsewhere in the test? Or do I need to need to access this in the self.game.deal()
method somehow?