本文整理匯總了Python中unittest.mock.assert_called_once_with方法的典型用法代碼示例。如果您正苦於以下問題:Python mock.assert_called_once_with方法的具體用法?Python mock.assert_called_once_with怎麽用?Python mock.assert_called_once_with使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類unittest.mock
的用法示例。
在下文中一共展示了mock.assert_called_once_with方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_assert_called_once_with
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_once_with [as 別名]
def test_assert_called_once_with(self):
mock = Mock()
mock()
# Will raise an exception if it fails
mock.assert_called_once_with()
mock()
self.assertRaises(AssertionError, mock.assert_called_once_with)
mock.reset_mock()
self.assertRaises(AssertionError, mock.assert_called_once_with)
mock('foo', 'bar', baz=2)
mock.assert_called_once_with('foo', 'bar', baz=2)
mock.reset_mock()
mock('foo', 'bar', baz=2)
self.assertRaises(
AssertionError,
lambda: mock.assert_called_once_with('bob', 'bar', baz=2)
)
示例2: test_480_vm_device_attach
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_once_with [as 別名]
def test_480_vm_device_attach(self):
self.vm.add_handler('device-list:testclass', self.device_list_testclass)
mock_attach = unittest.mock.Mock()
mock_attach.return_value = None
del mock_attach._is_coroutine
self.vm.add_handler('device-attach:testclass', mock_attach)
with unittest.mock.patch.object(qubes.vm.qubesvm.QubesVM,
'is_halted', lambda _: False):
value = self.call_mgmt_func(b'admin.vm.device.testclass.Attach',
b'test-vm1', b'test-vm1+1234')
self.assertIsNone(value)
mock_attach.assert_called_once_with(self.vm, 'device-attach:testclass',
device=self.vm.devices['testclass']['1234'],
options={})
self.assertEqual(len(self.vm.devices['testclass'].persistent()), 0)
self.app.save.assert_called_once_with()
示例3: test_483_vm_device_attach_persistent
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_once_with [as 別名]
def test_483_vm_device_attach_persistent(self):
self.vm.add_handler('device-list:testclass', self.device_list_testclass)
mock_attach = unittest.mock.Mock()
mock_attach.return_value = None
del mock_attach._is_coroutine
self.vm.add_handler('device-attach:testclass', mock_attach)
with unittest.mock.patch.object(qubes.vm.qubesvm.QubesVM,
'is_halted', lambda _: False):
value = self.call_mgmt_func(b'admin.vm.device.testclass.Attach',
b'test-vm1', b'test-vm1+1234', b'persistent=yes')
self.assertIsNone(value)
dev = self.vm.devices['testclass']['1234']
mock_attach.assert_called_once_with(self.vm, 'device-attach:testclass',
device=dev,
options={})
self.assertIn(dev, self.vm.devices['testclass'].persistent())
self.app.save.assert_called_once_with()
示例4: test_485_vm_device_attach_options
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_once_with [as 別名]
def test_485_vm_device_attach_options(self):
self.vm.add_handler('device-list:testclass', self.device_list_testclass)
mock_attach = unittest.mock.Mock()
mock_attach.return_value = None
del mock_attach._is_coroutine
self.vm.add_handler('device-attach:testclass', mock_attach)
with unittest.mock.patch.object(qubes.vm.qubesvm.QubesVM,
'is_halted', lambda _: False):
value = self.call_mgmt_func(b'admin.vm.device.testclass.Attach',
b'test-vm1', b'test-vm1+1234', b'option1=value2')
self.assertIsNone(value)
dev = self.vm.devices['testclass']['1234']
mock_attach.assert_called_once_with(self.vm, 'device-attach:testclass',
device=dev,
options={'option1': 'value2'})
self.app.save.assert_called_once_with()
示例5: test_490_vm_device_detach
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_once_with [as 別名]
def test_490_vm_device_detach(self):
self.vm.add_handler('device-list:testclass', self.device_list_testclass)
self.vm.add_handler('device-list-attached:testclass',
self.device_list_attached_testclass)
mock_detach = unittest.mock.Mock()
mock_detach.return_value = None
del mock_detach._is_coroutine
self.vm.add_handler('device-detach:testclass', mock_detach)
with unittest.mock.patch.object(qubes.vm.qubesvm.QubesVM,
'is_halted', lambda _: False):
value = self.call_mgmt_func(b'admin.vm.device.testclass.Detach',
b'test-vm1', b'test-vm1+1234')
self.assertIsNone(value)
mock_detach.assert_called_once_with(self.vm, 'device-detach:testclass',
device=self.vm.devices['testclass']['1234'])
self.app.save.assert_called_once_with()
示例6: test_652_vm_device_set_persistent_false
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_once_with [as 別名]
def test_652_vm_device_set_persistent_false(self):
self.vm.add_handler('device-list:testclass',
self.device_list_testclass)
assignment = qubes.devices.DeviceAssignment(self.vm, '1234', {},
True)
self.loop.run_until_complete(
self.vm.devices['testclass'].attach(assignment))
self.vm.add_handler('device-list-attached:testclass',
self.device_list_attached_testclass)
dev = qubes.devices.DeviceInfo(self.vm, '1234')
self.assertIn(dev, self.vm.devices['testclass'].persistent())
with unittest.mock.patch.object(qubes.vm.qubesvm.QubesVM,
'is_halted', lambda _: False):
value = self.call_mgmt_func(
b'admin.vm.device.testclass.Set.persistent',
b'test-vm1', b'test-vm1+1234', b'False')
self.assertIsNone(value)
self.assertNotIn(dev, self.vm.devices['testclass'].persistent())
self.assertIn(dev, self.vm.devices['testclass'].attached())
self.app.save.assert_called_once_with()
示例7: test_653_vm_device_set_persistent_true_unchanged
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_once_with [as 別名]
def test_653_vm_device_set_persistent_true_unchanged(self):
self.vm.add_handler('device-list:testclass',
self.device_list_testclass)
assignment = qubes.devices.DeviceAssignment(self.vm, '1234', {},
True)
self.loop.run_until_complete(
self.vm.devices['testclass'].attach(assignment))
self.vm.add_handler('device-list-attached:testclass',
self.device_list_attached_testclass)
with unittest.mock.patch.object(qubes.vm.qubesvm.QubesVM,
'is_halted', lambda _: False):
value = self.call_mgmt_func(
b'admin.vm.device.testclass.Set.persistent',
b'test-vm1', b'test-vm1+1234', b'True')
self.assertIsNone(value)
dev = qubes.devices.DeviceInfo(self.vm, '1234')
self.assertIn(dev, self.vm.devices['testclass'].persistent())
self.assertIn(dev, self.vm.devices['testclass'].attached())
self.app.save.assert_called_once_with()
示例8: test_assert_called_once_with_function_spec
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_once_with [as 別名]
def test_assert_called_once_with_function_spec(self):
def f(a, b, c, d=None):
pass
mock = Mock(spec=f)
mock(1, b=2, c=3)
mock.assert_called_once_with(1, 2, 3)
mock.assert_called_once_with(a=1, b=2, c=3)
self.assertRaises(AssertionError, mock.assert_called_once_with,
1, b=3, c=2)
# Expected call doesn't match the spec's signature
with self.assertRaises(AssertionError) as cm:
mock.assert_called_once_with(e=8)
self.assertIsInstance(cm.exception.__cause__, TypeError)
# Mock called more than once => always fails
mock(4, 5, 6)
self.assertRaises(AssertionError, mock.assert_called_once_with,
1, 2, 3)
self.assertRaises(AssertionError, mock.assert_called_once_with,
4, 5, 6)
示例9: test_existing_cache_with_unrelated_data
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_once_with [as 別名]
def test_existing_cache_with_unrelated_data(self):
section = Section('test-section')
filedict = {}
# Start with some unrelated cache values. Those are ignored as they are
# never hit during a cache lookup.
cache = {CustomTasksBear: {b'123456': [100, 101, 102]}}
task_args = -1, -2, -3
bear = CustomTasksBear(section, filedict, tasks=[task_args])
with unittest.mock.patch.object(bear, 'analyze',
wraps=bear.analyze) as mock:
# First time we have a cache miss.
results = self.execute_run({bear}, cache)
mock.assert_called_once_with(*task_args)
self.assertEqual(results, list(task_args))
self.assertEqual(len(cache), 1)
self.assertIn(CustomTasksBear, cache)
cache_values = next(iter(cache.values()))
self.assertEqual(len(cache_values), 2)
# The unrelated data is left untouched.
self.assertIn(b'123456', cache_values)
self.assertEqual(cache_values[b'123456'], [100, 101, 102])
示例10: test_create_autospec_classmethod_and_staticmethod
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_once_with [as 別名]
def test_create_autospec_classmethod_and_staticmethod(self):
class TestClass:
@classmethod
def class_method(cls):
pass
@staticmethod
def static_method():
pass
for method in ('class_method', 'static_method'):
with self.subTest(method=method):
mock_method = mock.create_autospec(getattr(TestClass, method))
mock_method()
mock_method.assert_called_once_with()
self.assertRaises(TypeError, mock_method, 'extra_arg')
#Issue21238
示例11: test_require_authentication_ok
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_once_with [as 別名]
def test_require_authentication_ok(
mocked_authenticator, configure_model, sudo_client_v1,
sudo_user_with_external_id, app):
now, expired = make_now_expired()
endpoint = "/" + pytest.faux.gen_alphanumeric()
mock = mocked_authenticator.client.tokens.get_token_data
mock.return_value = {
"token": make_token_data(sudo_user_with_external_id, now, expired)
}
@app.route(endpoint)
@mocked_authenticator.require_authentication
def testing_endpoint():
assert int(flask.g.token.expires_at.timestamp()) == int(
calendar.timegm(expired.timetuple()))
assert flask.g.token.user_id == sudo_user_with_external_id.model_id
return flask.jsonify(value=1)
response = sudo_client_v1.get(endpoint)
assert response.status_code == 200
assert response.json == {"value": 1}
mock = mocked_authenticator.client.tokens.get_token_data
mock.assert_called_once_with(sudo_client_v1.auth_token)
示例12: test_require_authentication_unknown_user
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_once_with [as 別名]
def test_require_authentication_unknown_user(
mocked_authenticator, configure_model, sudo_client_v1,
sudo_user_with_external_id, app):
now, expired = make_now_expired()
endpoint = "/" + pytest.faux.gen_alphanumeric()
mock = mocked_authenticator.client.tokens.get_token_data
mock.return_value = {
"token": make_token_data(sudo_user_with_external_id, now, expired)
}
mock.return_value["token"]["user"]["id"] += "FAKE"
@app.route(endpoint)
@mocked_authenticator.require_authentication
def testing_endpoint():
assert int(flask.g.token.expires_at.timestamp()) == int(
calendar.timegm(expired.timetuple()))
assert flask.g.token.user_id == sudo_user_with_external_id.model_id
return flask.jsonify(value=1)
response = sudo_client_v1.get(endpoint)
assert response.status_code == 401
mock = mocked_authenticator.client.tokens.get_token_data
mock.assert_called_once_with(sudo_client_v1.auth_token)
示例13: test_require_authentication_no_keystone
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_once_with [as 別名]
def test_require_authentication_no_keystone(
mocked_authenticator, configure_model, sudo_client_v1,
sudo_user_with_external_id, app):
now, expired = make_now_expired()
endpoint = "/" + pytest.faux.gen_alphanumeric()
mocked_authenticator.client.tokens.get_token_data.side_effect = \
keystoneauth1.exceptions.ClientException
@app.route(endpoint)
@mocked_authenticator.require_authentication
def testing_endpoint():
assert int(flask.g.token.expires_at.timestamp()) == int(
calendar.timegm(expired.timetuple()))
assert flask.g.token.user_id == sudo_user_with_external_id.model_id
return flask.jsonify(value=1)
response = sudo_client_v1.get(endpoint)
assert response.status_code == 401
mock = mocked_authenticator.client.tokens.get_token_data
mock.assert_called_once_with(sudo_client_v1.auth_token)
示例14: test_no_source
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_once_with [as 別名]
def test_no_source(self, commands, mocker):
mock = mocker.patch('qutebrowser.config.configcommands.editor.'
'ExternalEditor._start_editor', autospec=True)
commands.config_edit(no_source=True)
mock.assert_called_once_with(unittest.mock.ANY)
示例15: test_with_sourcing
# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import assert_called_once_with [as 別名]
def test_with_sourcing(self, commands, config_stub, patch_editor):
assert config_stub.val.content.javascript.enabled
mock = patch_editor('c.content.javascript.enabled = False')
commands.config_edit()
mock.assert_called_once_with(unittest.mock.ANY)
assert not config_stub.val.content.javascript.enabled