From 1221c7107f631340ba186600845b46f44e014170 Mon Sep 17 00:00:00 2001 From: sColin16 Date: Sun, 10 Dec 2017 16:25:25 +0000 Subject: [PATCH] test_lib.py: Add mock_request_exception. --- zulip_bots/zulip_bots/test_lib.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/zulip_bots/zulip_bots/test_lib.py b/zulip_bots/zulip_bots/test_lib.py index fb5bb0f..3ddfae0 100755 --- a/zulip_bots/zulip_bots/test_lib.py +++ b/zulip_bots/zulip_bots/test_lib.py @@ -174,6 +174,10 @@ class StubBotTestCase(TestCase): http_data = read_bot_fixture_data(self.bot_name, test_name) return mock_http_conversation(http_data) + def mock_request_exception(self): + # type: () -> Any + return mock_request_exception() + def mock_config_info(self, config_info): # type: (Dict[str, str]) -> Any return patch('zulip_bots.test_lib.StubBotHandler.get_config_info', return_value=config_info) @@ -258,6 +262,19 @@ def mock_http_conversation(http_data): ['params', 'headers', 'json'] ) +@contextmanager +def mock_request_exception(): + # type: () -> Any + def assert_mock_called(mock_result): + # type: (Any) -> None + mock_result.assert_called() + + with patch('requests.get') as mock_get: + mock_get.return_value = True + mock_get.side_effect = requests.exceptions.RequestException + yield + assert_mock_called(mock_get) + class BotTestCase(StubBotTestCase): """Test class for common Bot test helper methods""" bot_name = '' # type: str