本文整理汇总了Python中mock.Mock.method方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.method方法的具体用法?Python Mock.method怎么用?Python Mock.method使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.Mock
的用法示例。
在下文中一共展示了Mock.method方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_serverCreate
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import method [as 别名]
def test_serverCreate(self):
mock = Mock()
mock.method = Mock(return_value=True)
actual = mock.method()
expected = True
self.assertEqual(actual, expected)
示例2: test_mock_assert_called_with_can_fail
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import method [as 别名]
def test_mock_assert_called_with_can_fail():
test_object = Mock()
test_object.method({'bob': 'barker'})
with pytest.raises(AssertionError):
test_object.method.assert_called_with(instance_of(list))
示例3: test_method_must_have_side_effects
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import method [as 别名]
def test_method_must_have_side_effects(self):
"""Method must have side effects."""
from ..csrf import CSRFError
mock_request = Mock()
mock_request.method = 'b'
validator = self.makeOne('token', target_methods=['a'])
retval = validator.validate(mock_request)
self.assertTrue(retval is None)
self.assertRaises(
AssertionError,
mock_request.headers.get.assert_called_with,
'X-Requested-With'
)
mock_request = Mock()
mock_request.method = 'a'
self.assertRaises(
CSRFError,
validator.validate,
mock_request
)
示例4: test_clientRead
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import method [as 别名]
def test_clientRead(self):
mock = Mock()
mock.method = Mock(return_value=True)
actual = mock.method()
expected = True
self.assertEqual(actual, expected)
示例5: test_spec__object
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import method [as 别名]
def test_spec__object():
mock = Mock(spec=Spec())
attrs = dir(mock)
assert 'class_attr' in attrs
assert '_private_attr' in attrs # constructor IS consulted
assert 'method' in attrs
mock.method('arg1', 'arg2') # signature is not respected
assert 'prop_ro' in attrs
mock.prop_ro = 'bla' # read-only is not respected
示例6: test_deny_create_on_put_permission
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import method [as 别名]
def test_deny_create_on_put_permission(self):
permission = DenyCreateOnPutPermission()
view = Mock()
request = Mock()
request.method = 'GET'
self.assertTrue(permission.has_permission(request, view))
request.method = 'PUT'
self.assertTrue(permission.has_permission(request, view))
request.method = 'PUT'
view.get_object = Mock(side_effect=Http404)
self.assertFalse(permission.has_permission(request, view))
示例7: test_mock_method_calls
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import method [as 别名]
def test_mock_method_calls(self):
mock = Mock()
mock()
mock.method().attribute.method()
mock.attribute.method()
# mock_calls: inclusive, recursive; split by '()', good for callable mocks.
self.assertEqual(mock.mock_calls, [
call(),
call.method(),
call.method().attribute.method(),
call.attribute.method()])
# method_calls: exclusive, recursive; terminated by '()', good for non-callable mocks.
self.assertEqual(mock.method_calls, [call.method(), call.attribute.method()])
示例8: prepare_request_mock
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import method [as 别名]
def prepare_request_mock(self, data, referer='http://localhost/user_with_workspaces/Public Workspace', user=None, extra_headers={}, GET=''):
request = Mock()
request.method = 'POST'
request.get_host.return_value = 'localhost'
GET_PARAMETERS = parse_qsl(GET)
request.GET = MagicMock()
request.GET.__len__.side_effect = lambda: len(GET_PARAMETERS)
request.GET.__getitem__.side_effect = lambda key: GET_PARAMETERS[key]
request.GET.urlencode.side_effect = lambda: GET
request.COOKIES = {
settings.SESSION_COOKIE_NAME: 'test',
}
request.META = {
'HTTP_ACCEPT': 'application/json',
'SERVER_PROTOCOL': 'http',
'REMOTE_ADDR': '127.0.0.1',
'content_type': 'application/json',
'content_length': len(data),
'HTTP_HOST': 'localhost',
'HTTP_REFERER': referer,
'HTTP_X_FI_WARE_OAUTH_TOKEN': 'true',
}
request.META.update(extra_headers)
request.read.return_value = data
if user is None:
request.user = self.admin_mock
else:
request.user = user
return request
示例9: has_object_perm
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import method [as 别名]
def has_object_perm(self, auth, user, obj, method="GET"):
perm = OrganizationPermission()
request = Mock()
request.auth = auth
request.user = user
request.method = method
return perm.has_object_permission(request, None, obj)
示例10: test_process_request_empty_header
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import method [as 别名]
def test_process_request_empty_header(self):
request = Mock(path="/")
request.method = "OPTIONS"
request.META = {"HTTP_ACCESS_CONTROL_REQUEST_METHOD": ""}
with settings_override(CORS_URLS_REGEX="^.*$"):
response = self.middleware.process_request(request)
self.assertIsInstance(response, HttpResponse)
示例11: test_get_bounding_box_of_country
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import method [as 别名]
def test_get_bounding_box_of_country(self):
request = Mock()
request.method = 'GET'
request.GET.get.return_value = MockGeoserver().response()
geonames_service = Mock()
geoserver = Mock()
geoserver.get_admin_units_for_country.return_value = '["Districts", "County"]'
geonames_service.query_for_country.return_value = '''
{"geonames"
:[{"countryName":"India",
"bBoxWest":68.1866760253906,
"currencyCode":"INR",
"fipsCode":"IN",
"countryCode":"IN",
"isoNumeric":"356",
"capital":"New Delhi",
"areaInSqKm":"3287590.0",
"languages":"en-IN,hi,bn,te,mr,ta,ur,gu,ml,kn,or,pa,as,ks,sd,sa,ur-IN",
"bBoxEast":97.4033126831055,
"isoAlpha3":"IND",
"continent":"AS",
"bBoxNorth":35.5042304992676,
"geonameId":1269750,
"bBoxSouth":6.74713850021362,
"population":"1147995000"}]}'''
response = views.country_details(request, geonames_service, geoserver)
self.assertEquals('{"north": 180, "west": -90, "admin_units": "", "country": "Request failed", "east": 90, "adm1": [], "south": -180}', response.content)
示例12: test_fakesock_socket_sendall_with_body_data_with_chunked_entry
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import method [as 别名]
def test_fakesock_socket_sendall_with_body_data_with_chunked_entry(POTENTIAL_HTTP_PORTS, old_socket, match_uriinfo):
("fakesock.socket#sendall should call real_sendall when not ")
# Background:
# Using a subclass of socket that mocks out real_sendall
class MySocket(fakesock.socket):
def real_sendall(self, data):
raise AssertionError('should have never been called')
matcher = Mock(name='matcher')
info = Mock(name='info')
httpretty.match_uriinfo.return_value = (matcher, info)
# Using a mocked entry
entry = Mock()
entry.method = 'GET'
entry.info.path = '/foo'
entry.request.headers = {
'transfer-encoding': 'chunked',
}
entry.request.body = b''
# Given an instance of that socket
socket = MySocket()
socket._entry = entry
# And that is is considered http
socket.connect(('foo.com', 80))
# When I try to send data
socket.sendall(b"BLABLABLABLA")
# Then the entry should have that body
httpretty.last_request.body.should.equal(b'BLABLABLABLA')
示例13: has_object_perm
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import method [as 别名]
def has_object_perm(self, auth, user, obj, method='GET'):
perm = ProjectPermission()
request = Mock()
request.auth = auth
request.user = user
request.method = method
return perm.has_object_permission(request, None, obj)
示例14: get_signed_grade_mock_request_with_correct_signature
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import method [as 别名]
def get_signed_grade_mock_request_with_correct_signature(self):
"""
Generate a proper LTI request object
"""
mock_request = Mock()
mock_request.headers = {
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': (
u'OAuth realm="https://testurl/", oauth_body_hash="wwzA3s8gScKD1VpJ7jMt9b%2BMj9Q%3D",'
'oauth_nonce="18821463", oauth_timestamp="1409321145", '
'oauth_consumer_key="__consumer_key__", oauth_signature_method="HMAC-SHA1", '
'oauth_version="1.0", oauth_signature="fHsE1hhIz76/msUoMR3Lyb7Aou4%3D"'
)
}
mock_request.url = u'https://testurl'
mock_request.http_method = u'POST'
mock_request.method = mock_request.http_method
mock_request.body = (
'<?xml version=\'1.0\' encoding=\'utf-8\'?>\n'
'<imsx_POXEnvelopeRequest xmlns="http://www.imsglobal.org/services/ltiv1p1/xsd/imsoms_v1p0">'
'<imsx_POXHeader><imsx_POXRequestHeaderInfo><imsx_version>V1.0</imsx_version>'
'<imsx_messageIdentifier>edX_fix</imsx_messageIdentifier></imsx_POXRequestHeaderInfo>'
'</imsx_POXHeader><imsx_POXBody><replaceResultRequest><resultRecord><sourcedGUID>'
'<sourcedId>MITxLTI/MITxLTI/201x:localhost%3A8000-i4x-MITxLTI-MITxLTI-lti-3751833a214a4f66a0d18f63234207f2:363979ef768ca171b50f9d1bfb322131</sourcedId>' # pylint: disable=line-too-long
'</sourcedGUID><result><resultScore><language>en</language><textString>0.32</textString></resultScore>'
'</result></resultRecord></replaceResultRequest></imsx_POXBody></imsx_POXEnvelopeRequest>'
)
return mock_request
示例15: test_process_request_empty_header
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import method [as 别名]
def test_process_request_empty_header(self):
request = Mock(path='/')
request.method = 'OPTIONS'
request.META = {'HTTP_ACCESS_CONTROL_REQUEST_METHOD': ''}
with settings_override(CORS_URLS_REGEX='^.*$'):
response = self.middleware.process_request(request)
self.assertIsInstance(response, HttpResponse)