2 回答

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊
通過覆蓋實(shí)例的foo方法來修復(fù)它
import unittest
from unittest.mock import patch
from foo import Beef
class TestBeef(unittest.TestCase):
@patch('foo.Foo')
def test_run(self, mock_foo):
beef = Beef()
beef.foo.bar.return_value = {'a': 'x'}
assert result == {'a': 'x'}

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊
在你的test_foo.py:
import unittest
from unittest import mock
from foo import Beef, Foo
class TestBeef(unittest.TestCase):
def test_run(self):
mock.object.patch(Foo, 'bar', return_value={'a': 'x'})
beef = Beef()
assert result == {'a': 'x'}
添加回答
舉報(bào)