本文整理汇总了Python中unittest.mock.Mock.return_value方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.return_value方法的具体用法?Python Mock.return_value怎么用?Python Mock.return_value使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock.Mock
的用法示例。
在下文中一共展示了Mock.return_value方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _setup_mocks
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import return_value [as 别名]
def _setup_mocks(self, mock_pygame):
mock_screen = Mock()
mock_pygame.display.get_surface.return_value = mock_screen
mock_edges = Mock()
mock_edges.left.rect.x = 0
mock_edges.left.rect.width = 15
mock_edges.top.rect.y = 150
mock_edges.top.rect.height = 15
mock_create_edges = Mock()
mock_create_edges.return_value = mock_edges
mock_background = Mock()
mock_create_background = Mock()
mock_create_background.return_value = mock_background
mock_create_bricks = Mock()
BaseRound._create_edges = mock_create_edges
BaseRound._create_background = mock_create_background
BaseRound._create_bricks = mock_create_bricks
mock_image, mock_rect = Mock(), Mock()
mock_brick = Mock()
mock_brick.image = mock_image
mock_brick.rect = mock_rect
mock_brick.rect.width = 42
mock_brick.rect.height = 21
return mock_screen, mock_background, mock_brick
示例2: test_fork_pipeline
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import return_value [as 别名]
def test_fork_pipeline(self):
entry = {'i': 1}
initial_step = Mock()
initial_step.return_value = entry
def initial_left(entry):
entry['x'] = 1
return entry
def initial_right(entry):
entry['y'] = 2
return entry
left = Mock()
left.return_value = entry
right = Mock()
right.return_value = entry
left_branch = [initial_left, left]
right_branch = [initial_right, right]
forked_pipeline = pipeline.pipeline([entry],
initial_step,
pipeline.fork(left_branch, right_branch))
pipeline.consume(forked_pipeline)
self.assertTrue(left.called)
self.assertEqual(left.call_args[0][0], {'x': 1, 'i': 1})
self.assertTrue(right.called)
self.assertEqual(right.call_args[0][0], {'y': 2, 'i': 1})
示例3: test_aggiornamento_ultima_azione
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import return_value [as 别名]
def test_aggiornamento_ultima_azione(self):
persona = crea_persona()
utente = crea_utenza(persona)
utente.is_staff = True
utente.richiedi_2fa = True
utente.save()
data_base = datetime.datetime(2016, 1, 11, 12, 34, 56)
data_1 = data_base + datetime.timedelta(seconds=60)
data_2 = data_base + datetime.timedelta(seconds=120)
data_3 = data_base + datetime.timedelta(seconds=180)
data_4 = data_base + datetime.timedelta(seconds=240)
mock_time = Mock()
mock_time.return_value = data_base
with patch('autenticazione.two_factor.middleware.now', return_value=mock_time()):
self.assertIsNone(utente.ultima_azione)
response = self.client.post(settings.LOGIN_URL, data={
'auth-username': utente.email, 'auth-password': 'prova',
'jorvik_login_view-current_step': 'auth'
})
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, reverse('two_factor:profile'))
utente = self._ricarica_model(utente._meta.model, utente)
self.assertEqual(utente.ultima_azione, data_base)
mock_time.return_value = data_1
with patch('autenticazione.two_factor.middleware.now', return_value=mock_time()):
response = self.client.post(settings.LOGOUT_URL)
self.assertContains(response, 'Sei uscito da Gaia')
utente = self._ricarica_model(utente._meta.model, utente)
self.assertEqual(utente.ultima_azione, data_1)
mock_time.return_value = data_2
with patch('autenticazione.two_factor.middleware.now', return_value=mock_time()):
StaticDevice.objects.create(user=utente, name="Device")
response = self.client.post(settings.LOGIN_URL, data={
'auth-username': utente.email, 'auth-password': 'prova',
'jorvik_login_view-current_step': 'auth'
})
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, '/utente/', fetch_redirect_response=False)
utente = self._ricarica_model(utente._meta.model, utente)
self.assertEqual(utente.ultima_azione, data_2)
mock_time.return_value = data_3
with patch('autenticazione.two_factor.middleware.now', return_value=mock_time()):
response = self.client.get('/utente/')
utente = self._ricarica_model(utente._meta.model, utente)
self.assertEqual(utente.ultima_azione, data_3)
mock_time.return_value = data_4
with patch('autenticazione.two_factor.middleware.now', return_value=mock_time()):
response = self.client.get(reverse('two_factor:profile'))
utente = self._ricarica_model(utente._meta.model, utente)
self.assertEqual(utente.ultima_azione, data_4)
示例4: test_project_init_errors_2
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import return_value [as 别名]
def test_project_init_errors_2(project_file_1_fixture):
op = Mock()
op.side_effect = raiser(PermissionError)
op.return_value = project_file_1_fixture
with patch('srmlf.project.open', op, create=True):
isfile = Mock()
isfile.return_value = True
with patch('os.path.isfile', isfile, create=True):
with pytest.raises(project.ProjectFileUnreadableException):
project.Project('test')
示例5: project_1_fixture
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import return_value [as 别名]
def project_1_fixture():
fd = project_file_1_fixture()
op = Mock()
op.return_value = fd
with patch('srmlf.project.open', op, create=True):
isfile = Mock()
isfile.return_value = True
with patch('os.path.isfile', isfile, create=True):
p = project.Project('test')
return p
示例6: test_multiple_project_files
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import return_value [as 别名]
def test_multiple_project_files():
op = Mock()
op.return_value = project_file_1_fixture
with patch('srmlf.project.open', op, create=True):
isfile = Mock()
isfile.return_value = False
with patch('os.path.isfile', isfile, create=True):
glob = Mock()
glob.return_value = ['test_(1000).csv', 'test_(10000).csv']
with patch('glob.glob', glob, create=True):
with pytest.raises(project.ProjectDuplicateException):
project.Project('test')
示例7: test_corrupt_file
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import return_value [as 别名]
def test_corrupt_file():
isfile = Mock()
isfile.return_value = True
with patch('os.path.isfile', isfile, create=True):
op = Mock()
s = StringIO()
s.write('nope')
s.seek(0)
op.return_value = s
with patch('srmlf.project.open', op, create=True):
with pytest.raises(project.CorruptedProjectException):
project.Project('test')
示例8: project_2_fixture
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import return_value [as 别名]
def project_2_fixture():
fd = project_file_1_fixture()
op = Mock()
op.return_value = fd
with patch('srmlf.project.open', op, create=True):
isfile = Mock()
isfile.return_value = False
with patch('os.path.isfile', isfile, create=True):
glob = Mock()
glob.return_value = ['test_(100).csv']
with patch('glob.glob', glob, create=True):
p = project.Project('test')
return p
示例9: test_functions_are_called
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import return_value [as 别名]
def test_functions_are_called(self):
initial_arg = {}
def generator():
yield initial_arg
first_function = Mock()
first_function.return_value = initial_arg
second_function = Mock()
second_function.return_value = initial_arg
pipeline.consume(pipeline.pipeline(generator(), first_function, second_function))
self.assertTrue(first_function.called)
self.assertEqual(first_function.call_args[0][0], initial_arg)
self.assertTrue(second_function.called)
self.assertEqual(second_function.call_args[0][0], initial_arg)
示例10: test_current_user_can_on_submission
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import return_value [as 别名]
def test_current_user_can_on_submission(monkeypatch, database):
submission = SubmissionFactory()
user = UserFactory()
database.session.add_all([submission, user])
database.session.commit()
current_user_can = Mock()
monkeypatch.setattr(permissions, 'g', Mock(user=user))
monkeypatch.setattr(permissions, 'current_user_can', current_user_can)
current_user_can.return_value = True
assert permissions.current_user_can_on_submission('reader', submission)
current_user_can.return_value = False
assert not permissions.current_user_can_on_submission('reader', submission)
submission.user_id = user.user_id
assert permissions.current_user_can_on_submission('reader', submission)
示例11: test_with_pyoidc
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import return_value [as 别名]
def test_with_pyoidc(self):
responses.add(responses.POST,
"https://graph.facebook.com/v2.5/oauth/access_token",
body=json.dumps({"access_token": "qwerty",
"token_type": "bearer",
"expires_in": 9999999999999}),
adding_headers={"set-cookie": "TEST=testing; path=/"},
status=200,
content_type='application/json')
responses.add(responses.GET,
"https://graph.facebook.com/v2.5/me",
match_querystring=False,
body=json.dumps(FB_RESPONSE),
status=200,
content_type='application/json')
context = Context()
context.path = 'facebook/sso/redirect'
context.state = State()
internal_request = InternalRequest(UserIdHashType.transient, 'http://localhost:8087/sp.xml')
get_state = Mock()
get_state.return_value = STATE
resp = self.fb_backend.start_auth(context, internal_request, get_state)
context.cookie = resp.headers[0][1]
context.request = {
"code": FB_RESPONSE_CODE,
"state": STATE
}
self.fb_backend.auth_callback_func = self.verify_callback
self.fb_backend.authn_response(context)
示例12: test_route_request_with_id
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import return_value [as 别名]
def test_route_request_with_id(self, mock_import_module):
# Setup
mock_module = Mock()
mock_get = Mock()
mock_get.return_value = "raw data response"
mock_module.get = mock_get
mock_import_module.return_value = mock_module
server.CONFIG.get.return_value = '{"test_api": ["GET"]}'
self.mock_request.method = 'GET'
# Preconditions
# Run the test
response = server.route_request("test_api/123.json")
# Postconditions
self.assertEqual(self.mock_abort.call_count, 0)
self.assertEqual(response, "raw data response")
self.assertEqual(self.mock_connection.close.call_count, 1)
get_call_args = mock_get.call_args[0][1]
self.assertDictEqual(get_call_args, {
'id': '123'
})
示例13: test_route_request_with_missing_method
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import return_value [as 别名]
def test_route_request_with_missing_method(self, mock_import_module):
# Setup
mock_module = Mock()
mock_post = Mock()
mock_post.return_value = "raw data response"
mock_module.post = mock_post
mock_module.get = None
mock_import_module.return_value = mock_module
server.CONFIG.get.return_value = '{"test_api": ["GET"]}'
self.mock_request.method = 'GET'
self.mock_request.params.decode.return_value = {
'request_data': 'here'
}
# Preconditions
# Run the test
response = server.route_request("test_api.json")
# Postconditions
self.assertEqual(self.mock_abort.call_count, 1)
self.assertEqual(self.mock_abort.call_args[0][0], 503)
self.assertEqual(response, None)
self.assertEqual(self.mock_database.call_count, 0)
示例14: test_route_request_with_bad_output_format
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import return_value [as 别名]
def test_route_request_with_bad_output_format(self, mock_import_module):
# Setup
mock_module = Mock()
mock_post = Mock()
mock_post.return_value = "raw data response"
mock_module.post = mock_post
mock_import_module.return_value = mock_module
server.CONFIG.get.return_value = '{"test_api": ["POST"]}'
self.mock_request.method = 'POST'
self.mock_request.params.decode.return_value = {
'request_data': 'here'
}
# Preconditions
# Run the test
response = server.route_request("test_api.xml")
# Postconditions
self.assertEqual(self.mock_abort.call_count, 1)
self.assertEqual(self.mock_abort.call_args[0][0], 405)
self.assertEqual(response, None)
self.assertEqual(self.mock_database.call_count, 0)
示例15: test_route_request_with_request_data_and_id_and_path
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import return_value [as 别名]
def test_route_request_with_request_data_and_id_and_path(self, mock_import_module):
# Setup
mock_module = Mock()
mock_post = Mock()
mock_post.return_value = "raw data response"
mock_module.post = mock_post
mock_import_module.return_value = mock_module
server.CONFIG.get.return_value = '{"test_api": ["POST"]}'
self.mock_request.method = 'POST'
self.mock_request.params.decode.return_value = {
'request_data': 'here'
}
# Preconditions
# Run the test
response = server.route_request("some_id/123/test_api/999.json")
# Postconditions
self.assertEqual(self.mock_abort.call_count, 0)
self.assertEqual(response, "raw data response")
self.assertEqual(self.mock_connection.close.call_count, 1)
get_call_args = mock_post.call_args[0][1]
self.assertDictEqual(get_call_args, {
'some_id': '123',
'id': '999',
'request_data': 'here'
})