I am trying to unit test that a function is called within another function.
def run_this_function(something):
'FOLD', {}
def go_to_the_right_street(action):
if street_we_are_on == 'pre_flop_play':
action, extra_information = run_this_function(something)
And my unit test looks like:
def test_go_to_the_right_street(street_we_are_on):
with patch('pb.app.run_this_function') as mocked_function:
actual = go_to_the_right_street('some_value')
# mocked_function.return_value = 'FOLD', {} tried this too but I get the same error
mocked_function.assert_called_once()
When I run the above, I get:
ValueError: not enough values to unpack (expected 2, got 0)
I have created the following structure on my filesystem:
project
|-pb
| |-app.py
|-test_app.py
File app.py
:
def run_this_function(something):
return 'FOLD', {}
def go_to_the_right_street(street_we_are_on):
if street_we_are_on == 'pre_flop_play':
action, extra_information = run_this_function('something') <--- added the string definition for 'something' instead something as variable
In this file is present only a modification (see the comment in it).
File test_app.py
import unittest
from unittest.mock import patch
from pb.app import go_to_the_right_street
# I have defined this function to substitute your function run_this_function()
def run_this_function_patch(something):
return 'FOLD', {}
class MyTestCase(unittest.TestCase):
def test_go_to_the_right_street(self):
with patch('pb.app.run_this_function') as mocked_function:
mocked_function.side_effect = [run_this_function_patch('something')] # <--- added this side_effect to return 'FOLD', {}
actual = go_to_the_right_street('pre_flop_play')
# mocked_function.return_value = 'FOLD', {} tried this too but I get the same error
mocked_function.assert_called_once()
if __name__ == '__main__':
unittest.main()
The 2 most important changes:
run_this_function_patch()
which is used to define the side_effect
; note the presence of the parenthesis []
and ()
mocked_function.side_effect = [run_this_function_patch('something')]
is defined before the calling of the function go_to_the_right_street()
With this code the output of the execution is the following:
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK