IMHO this is a good question and saying "don't use isinstance
, use duck typing instead" is a bad answer. Duck typing is great, but not a silver bullet. Sometimes isinstance
is necessary, even if it is not pythonic. For instance, if you work with some library or legacy code that isn't pythonic you must play with isinstance
. It is just the real world and mock was designed to fit this kind of work.
In the code the big mistake is when you write:
@patch('__main__.HelloWorld', spec=HelloWorld)
def test_mock(self,MK):
From patch
documentation we read (emphasize is mine):
Inside the body of the function or with statement, the target is patched with a new object.
That means when you patch the HelloWorld
class object the reference to HelloWorld
will be replaced by a MagicMock
object for the context of the test_mock()
function.
Then, when i_call_hello_world()
is executed in if isinstance(hw_obj, HelloWorld):
HelloWorld
is a MagicMock()
object and not a class (as the error suggests).
That behavior is because as a side effect of patching a class reference the 2nd argument of isinstance(hw_obj, HelloWorld)
becomes an object (a MagicMock
instance). This is neither a class
or a type
. A simple experiment to understand this behavior is to modify i_call_hello_world()
as follows:
HelloWorld_cache = HelloWorld
def i_call_hello_world(hw_obj):
print 'here... check type: %s' %type(HelloWorld_cache)
if isinstance(hw_obj, HelloWorld_cache):
print hw_obj.say_it()
The error will disappear because the original reference to HelloWorld
class is saved in HelloWorld_cache
when you load the module. When the patch is applied it will change just HelloWorld
and not HelloWorld_cache
.
Unfortunately, the previous experiment doesn't give us any way to play with cases like yours because you cannot change the library or legacy code to introduce a trick like this. Moreover, these are that kind of tricks that we would like to never see in our code.
The good news is that you can do something ,but you cannot just patch
the HelloWord
reference in the module where you have isinstace(o,HelloWord)
code to test. The best way depends on the real case that you must solve. In your example you can just create a Mock
to use as HelloWorld
object, use spec
argument to dress it as HelloWorld
instance and pass the isinstance
test. This is exactly one of the aims for which spec
is designed. Your test would be written like this:
def test_mock(self):
MK = MagicMock(spec=HelloWorld) #The hw_obj passed to i_call_hello_world
print type(MK)
MK.say_it.return_value = 'I am fake'
v = i_call_hello_world(MK)
print v
And the output of just unittest part is
<class 'mock.MagicMock'>
here... check type: <type 'type'>
I am fake
None