別のモジュールで関数を呼び出す単純なメソッドを置き換えるのに問題があります。私がモッキングについて理解していることから、呼び出されているメソッドを参照する必要があります(元のメソッドではなく、そのコンテキストで)。以下は、私が実行しているものの簡略化されたバージョンであり、モックについて学ぶ必要があるのは簡単なものであることを願っています. パッチは Class および Class メソッドのみに使用することを意図していますか、それともここで何か間違ったことをしていますか?
ありがとう、スティーブ
myapp.models.py
from myapp.backends import get_backend
class BasicClass(models.Model):
@staticmethod
def basic_method()
be = get_backend()
print be
myapp.backends._ init _.py
def get_backend():
return 'original value'
test.py
# Referencing the import in myapp.models.basic_class
# vs directly importing myapp.backends
# as indicated here:
# http://www.voidspace.org.uk/python/mock/patch.html#where-to-patch
from myapp.models import get_backend
from myapp.models.basic_class import BasicClass
class ParsersTest(TestCase):
@patch('myapp.models.get_backend')
def test_simplified(self, moves_backend):
# Assertion fails
assert get_backend is moves_backend
# Assuming that assertion fails is why the original return value is always returned
moves_backend.return_value = 'new return value'
BasicClass.basic_method()