views:

137

answers:

2

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  A: 

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 )
S.Lott
Yeah I was hoping to get away with not modifying the original class, may not be possible though.
Harley
You can't mock something that doesn't exist. The original class could not have the property set. You cannot mock that feature because it does not exist.
S.Lott
+1  A: 

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.

brotchie