Using mock patch to mock an instance method(使用 mock patch 模拟实例方法)
本文介绍了使用 mock patch 模拟实例方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在测试 Django 应用程序时尝试模拟一些东西,使用富有想象力的名称 模拟测试库.我似乎无法让它工作,我正在尝试这样做:
I'm trying to mock something while testing a Django app using the imaginatively named Mock testing library. I can't seem to quite get it to work, I'm trying to do this:
models.py
from somelib import FooClass
class Promotion(models.Model):
foo = models.ForeignKey(FooClass)
def bar(self):
print "Do something I don't want!"
test.py
class ViewsDoSomething(TestCase):
view = 'my_app.views.do_something'
def test_enter_promotion(self):
@patch.object(my_app.models.FooClass, 'bar')
def fake_bar(self, mock_my_method):
print "Do something I want!"
return True
self.client.get(reverse(view))
我做错了什么?
推荐答案
啊,我对在哪里应用补丁装饰器感到困惑.固定:
Ah I was confused on where to apply that patch decorator. Fixed:
class ViewsDoSomething(TestCase):
view = 'my_app.views.do_something'
@patch.object(my_app.models.FooClass, 'bar')
def test_enter_promotion(self, mock_method):
self.client.get(reverse(view))
这篇关于使用 mock patch 模拟实例方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:使用 mock patch 模拟实例方法
基础教程推荐
猜你喜欢
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 求两个直方图的卷积 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 包装空间模型 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
