【发布时间】:2017-07-08 20:15:05
【问题描述】:
我有一个发起用户登录请求的视图函数
看起来像这样:
def initiate_login(request):
# get request parameters
return check_user_and_send_otp(login_id)
然后请求由另一个函数处理
def check_user_and_send_otp(login_id):
# check if user exits
return send_otp_to_user(phone_number)
然后是另一个函数
def send_otp_to_user(phone_number):
# sends a message to user
return response
问题是在测试我的代码时,我不想在测试时向电话号码发送消息。
我的登录测试函数看起来有点像这样,是否可以在不更改代码的情况下模拟它?
def test_login_initiator(self):
response = self.client.post(self.login_url, data=self.login_data, content_type="application/json", **self.headers)
self.assertEqual(response.status_code, 200)
所有这些被其他人调用的函数都位于单独的模块中
【问题讨论】:
标签: python django unit-testing mocking