2013-11-14 20 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

相关问题