Skip to content
Advertisement

mocking/patching the value of a computed attribute from a classmethod

I am attempting to write a test for a function which calls an object’s classmethod — this classmethod goes on to return a new instance of that class.

There are plenty of examples of patching class attributes both here on stackoverflow and elsewhere but I am having difficulty understanding how to patch attribute/value such that I can test my function. I’ve referred to this answer.

Essentially I am trying to patch the attribute xxxx of the instance of Foo (within myFn) so that I can test/assert the subsequent value from its call to some_other_function()

The code below is standalone a ‘runnable’ of the problem: I’m getting an AttributeError: Foo doesn’t have the attribute ‘xxxx’

JavaScript

Very grateful for anyone’s help!

Advertisement

Answer

You should use the create parameter that will force the creation of the attribute if it does not exist:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement