1

Here is my class:

class GoogleCloudLayer:

    def deleteMachine(self, machineName):
        return machineName + ' is dead. (stubbed)'

It works:

>>> gc = GoogleCloudLayer()
>>> gc.deleteMachine('test')
test is dead (stubbed)

But I want to use in test and I want to have the assert_called_with method defined on it:

from mock import MagicMock
#Stubbing with itself just so it will have the `assert_called_with` method defined on it
GoogleCloudLayer.deleteMachine = MagicMock(side_effect = GoogleCloudLayer.deleteMachine)

But then I get

>>> gc = GoogleCloudLayer()
>>> gc.deleteMachine('test')
unbound method deleteMachine() must be called with GoogleCloudLayer instance as first argument (got str instance instead)

If I change production code to gc.deleteMachine(gc, 'test') it works. But we don't want that , do we?

WebQube
  • 8,510
  • 12
  • 51
  • 93

1 Answers1

1

lambda can be useful in this situation, try:

GoogleCloudLayer.createMachine = MagicMock(side_effect = lambda *args, **kwargs: GoogleCloudLayer.createMachine)
CosminO
  • 5,018
  • 6
  • 28
  • 50
nikniknik2016
  • 348
  • 2
  • 8