ฉันกำลังพยายามแก้ไขตัวแปรในโมดูล settings
ระหว่างการทดสอบวิธีรัน:
from unittest import mock
class Test(...):
@mock.patch('settings.TARGET_SCORES_PER_SECTION', True)
def test_register_user(self):
ฉันได้รับข้อผิดพลาดนี้:
ERROR: tests.test_user.transplant_class.<locals>.C (test_register_user)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/mock.py", line 1179, in patched
return func(*args, **keywargs)
TypeError: test_register_user() missing 1 required positional argument: 'self'
ฉันลองใช้วิธีอื่นแล้ว ไม่พบวิธีแก้ไข
ผมทำอะไรผิดหรือเปล่า?
เวอร์ชันนี้ใช้งานได้ดี:
def test_register_user(self):
with mock.patch('settings.TARGET_SCORES_PER_SECTION', True):
self._test_register_user()
def _test_register_user(self):
และฉันหวังว่าจะใช้เป็น มัณฑนากรก็จะทำงานในลักษณะเดียวกัน
settings
และเพิ่มการเยาะเย้ยที่แพตช์ให้กับพารามิเตอร์ฟังก์ชันทดสอบของคุณเช่นในdef test_register_user(self, mock_target_scores):
- person progmatico   schedule 19.04.2019as
ในคำสั่งwith
ซึ่งหมายความว่าคุณไม่ต้องการเข้าถึงการเยาะเย้ยนอกเหนือจากการสร้างแพตช์ด้วย ซึ่งก็ใช้ได้เช่นกันสำหรับกรณีจำลองธรรมดาๆ หรือหากคุณตั้งค่าอ็อบเจ็กต์อื่นให้ส่งผ่านไปยังแพตช์ด้วยnew
- person progmatico   schedule 19.04.2019self
ใน class method ไปครู่หนึ่ง? - person progmatico   schedule 19.04.2019