Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I mock a class property with mox?

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.

like image 254
Harley Holcombe Avatar asked Mar 25 '10 00:03

Harley Holcombe


2 Answers

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.

like image 190
brotchie Avatar answered Oct 11 '22 02:10

brotchie


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 )
like image 42
S.Lott Avatar answered Oct 11 '22 03:10

S.Lott