我正在学习模拟,我无法弄清楚为什么这不起作用......
@validate_captcha
def sendemail(request):
...
email.send()
return HttpResponse('Your email has been sent. Thank you for contacting us')
我有一个验证google recaptcha的包装器,我需要测试这个视图,但我无法弄清楚如何绕过包装器。
我试过了......
@patch('main.views.sendemail')
def test_sendmail_captcha_pass(self, mock_sendemail):
"""Testing the view after the captcha passing"""
mock_sendemail.return_value = True
things = Things()
data = {
'subject': 'test subject',
'name': 'john appleseed',
'email': 'john@apple.com',
'content': 'content of the message'}
post_url = reverse("main:contact_email")
result = things.client.post(post_url, {})
self.assertTrue(result.status_code == 401)
由于sendemail函数已经被包装,我使用@patch
的sendemail函数而不是validate_captcha函数,但我看不到下一步该做什么。
我尝试在shell中创建一个模拟对象并验证它确实提供了一个包装版本的sendemail函数