本文介绍了Python @patch无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试测试在其中创建另一个类的实例的方法,我正在尝试模拟内部类的创建……这对我不起作用.我尝试将问题简化为简化的案例-仍然行不通.这是简化的情况:
I am trying to test a method that within it creates an instance of another class, I am attempting to mock the creation of the inner class... That is not working for me. I tried replicating the problem to a simplified case - and still no go. Here is the simplified case:
我有一个名为pymock的目录,其中有一个__init__.py.除此之外,还有以下3个文件:
I have a directory named pymock with a __init__.py in it. Other than that there are these 3 files:
foo.py
#!/usr/bin/python class Foo(object): def foo1(self): return 1
goo.py
#!/usr/bin/python from foo import Foo class Goo(object): def goo1(self): f = Foo() return f.foo1()
goo_test.py
#!/usr/bin/python from mock import patch, Mock from nose.tools import assert_equal from goo import Goo class TestGoo(object): def setup(self): self.goo = Goo() @patch('pymock.foo.Foo', autospec=True) def test_goo1(self, foo1_mock): foo_instance = Mock() foo1_mock.return_value = foo_instance foo_instance.foo1.return_value = 11 assert_equal(11, self.goo.goo1())
提前谢谢!
推荐答案
您需要修补使用的名称.
@patch('pymock.goo.Foo', autospec=True)
这篇关于Python @patch无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!