How do I mock a class property with mox?

 ̄綄美尐妖づ 提交于 2019-12-10 14:10:27

问题


I have a class:

class MyClass(object):
    @property
    def myproperty(self):
        return 'hello'

Using mox and py.test, how do I mock out myproperty?

I've tried:

mock.StubOutWithMock(myclass, 'myproperty')
myclass.myproperty = 'goodbye'

and

mock.StubOutWithMock(myclass, 'myproperty')
myclass.myproperty.AndReturns('goodbye')

but both fail with AttributeError: can't set attribute.


回答1:


When stubbing out class attributes mox uses setattr. Thus

mock.StubOutWithMock(myinstance, 'myproperty')
myinstance.myproperty = 'goodbye'

is equivalent to

# Save old attribute so it can be replaced during teardown
saved = getattr(myinstance, 'myproperty')
# Replace the existing attribute with a mock
mocked = MockAnything()
setattr(myinstance, 'myproperty', mocked)

Note that because myproperty is a property getattr and setattr will be invoking the property's __get__ and __set__ methods, rather than actually "mocking out" the property itself.

Thus to get your desired outcome you just go one step deeper and mock out the property on the instance's class.

mock.StubOutWithMock(myinstance.__class__, 'myproperty')
myinstance.myproperty = 'goodbye'

Note that this might cause issues if you wish to concurrently mock multiple instances of MyClass with different myproperty values.




回答2:


Have you read about property? It's read-only, a "getter".

If you want a setter, you have two choices of how to create that.

Once you have both getter and setter, you can try again to mock out both of them.

class MyClass(object): # Upper Case Names for Classes.
    @property
    def myproperty(self):
        return 'hello'
    @myproperty.setter
    def myproperty(self,value):
        self.someValue= value

Or

class MyClass(object): # Upper Case Names for Classes.
    def getProperty(self):
        return 'hello'
    def setProperty(self,value):
        self.someValue= value
    myproperty= property( getProperty, setProperty )


来源:https://stackoverflow.com/questions/2512453/how-do-i-mock-a-class-property-with-mox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!