我无法为我的测试修补课程。我正在尝试修补属于 Scrapy 的东西——一个HtmlXpathSelector类。
这是一些代码:
from scrapy.selector import HtmlXPathSelector
from mock import MagicMock, patch
with patch('scrapy.selector.HtmlXPathSelector') as MockHtml:
instance = MockHtml.return_value
instance.method.return_value = 'foo'
example = HtmlXPathSelector()
print type(example)
assert example is instance
assert example.method == 'foo'
结果是:
<class 'scrapy.selector.lxmlsel.HtmlXPathSelector'>
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
AssertionError
>>>
此示例与 Mock 库教程中的示例几乎相同。知道为什么它不起作用吗?