-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
35 lines (27 loc) · 1.13 KB
/
test_client.py
File metadata and controls
35 lines (27 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import unittest
from unittest.mock import patch, MagicMock
from client import Client
class TestGuessingGameClient(unittest.TestCase):
@patch("guessing_game_client.socket")
def test_correct_guess_exits_game(self, mock_socket_module):
mock_socket_instance = MagicMock()
mock_socket_module.socket.return_value.__enter__.return_value = mock_socket_instance
#(setting up server responses)
mock_socket_instance.recv.side_effect = [
b'Too low!',
b'Too high!',
b'Correct! You win!'
]
#(simulating 3 guesses)
inputs = ['25', '75', '50']
input_iterator = iter(inputs)
def mock_input(prompt):
return next(input_iterator)
client = Client()
client.play(input_func=mock_input, socket_module=mock_socket_module)
#(check that sendall was called 3 times)
self.assertEqual(mock_socket_instance.sendall.call_count, 3)
#(check last message was "50")
mock_socket_instance.sendall.assert_called_with(b'50')
if __name__ == '__main__':
unittest.main()