Mocking nested properties with mock

Viewed 9351

I have a function call that returns an object:

r = Foo(x,y)

where r has a rich set of nested properties. For example, I can access r.prop_a.prop_b.prop_c. I would like to mock Foo, such that a specific leaf property of r is modified, i.e. such that r.prop_a.prop_b.prop_c returns a value under my control:

>> r = Foo(x,y)
>> r.prop_a.prop_b.prop_c
'fish'
>> # some mock magic patching of Foo is taking place here
>> r = Foo(x,y)
>> r.prop_a.prop_b.prop_c
'my_fish'

I do not care about intermediate properties much.

Is there an elegant way to mock nested properties with mock?

2 Answers
Related