简体   繁体   English

将可调用的MagicMock return_value实例方法更改为PropertyMock

[英]Change MagicMock return_value instance method callable to PropertyMock

I'm trying to change the callable for an instance method on my MagicMock to a PropertyMock as that is how it is accessed (Django model column). 我试图将我的MagicMock上的实例方法的可调用方法MagicMockPropertyMock因为这是如何访问它的方法(Django模型列)。 But test fails with 但是测试失败了

 _mock_self = <PropertyMock name='get().my_prop' id='4524632776'>

Given the following code 给出以下代码

# view
def get(self request):
    # stuff...
    obj = MyModel.objects.get(id=2)
    # more stuff...
    print(obj.my_prop)
    some_val = MyOtherModel(my_prop=obj.my_prop)

# tests
def test_my_test(mocker):
    other_obj = mocker.patch("app.views.MyOtherModel")
    obj_get = mocker.patch("app.views.MyModel.objects.get")
    obj_prop = mocker.PropertyMock(return_value=1)
    # This should translate to an instance of MyModel i.e. MyModel().my_prop
    obj_get.return_value.my_prop = obj_prop

    # run view

    obj_prop.assert_called_once() # Says never invoked
    # Failed as my_prop was a PropertyMock instead of actual value
    other_obj.assert_called_once_with(my_prop=obj_prop)

The print statement shows 打印声明显示

 <PropertyMock name='get().my_prop' id='4524632776'>

So it appears it is the same instance it just is not actually being invoked 因此看来,它是与实际上未被调用的相同实例

I also had attempted directly patching the property off the model but that does not appear to work as the Mocks are not the same. 我还尝试过直接从模型中修补该属性,但是由于Mocks不相同,因此似乎不起作用。

def test_my_test(mocker):
    other_obj = mocker.patch("app.views.MyOtherModel")
    obj_prop = mocker.patch("app.views.MyModel.my_prop", new_callable=mocker.PropertyMock)
    obj_get = mocker.patch("app.views.MyModel.objects.get")
    obj_get.return_value.my_prop = obj_prop

    # run view
    obj_prop.assert_called_once()
    # Failed as my_prop was a PropertyMock instead of actual value
    other_obj.assert_called_once_with(my_prop=obj_prop) 

Which fails with... 失败了...

_mock_self = <PropertyMock name='my_prop' id='4543022192'>

but prints 但是打印

 <MagicMock name='get().my_prop' id='4543752232'>

EDIT Added in other patches that more accurately depicted the implementation 编辑在其他补丁中添加,可以更准确地描述实现

Overwriting the desired property with a PropertyMock was unnecessary as I could simply assigned the value I wanted to the MagicMock . 不需要使用PropertyMock覆盖所需的属性,因为我只需将所需的值分配给MagicMock

def test_my_test(mocker):

    other_obj = mocker.patch("app.views.MyOtherModel")

    obj_get = mocker.patch("app.views.MyModel.objects.get")
    obj_get.return_value.my_prop = 2

    # run view
    other_obj.assert_called_once_with(my_prop=obj_get.return_value.my_prop) 

The .assert_called_once was used to check if the attribute was ever invoked and was added when an .assert_called_once_with originally failed. .assert_called_once用于检查是否曾经调用过该属性,并在最初失败的.assert_called_once_with失败时添加了该属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM