2013-11-14 22 views
2

我需要一些模擬幫助。如何在類中的方法中模擬外部函數

我在mymodule.py下面的代碼:

from someModule import external_function 

class Class1(SomeBaseClass): 
    def method1(self, arg1, arg2): 
     external_function(param) 

現在我有測試代碼:

import mock 
from django.test import TestCase 

from mymodule import class1 
class Class1Test(TestCase) 
    def test_method1: 
     '''how can I mock external_function here?''' 

回答

2

你可以寫成:

class Class1Test(TestCase): 

    @mock.patch('mymodule.external_function') 
    def test_method1(self, mock_external_function): 
     pass 

望着mymodule功能external_function直接導入。因此,您需要模擬mymodule.external_function,因爲這是在執行method1時將調用的函數。

+0

謝謝。不知何故,我嘲笑錯誤的東西。你把我弄直了。 – user2916464

相關問題