Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Patch Decorator - side effect with no parameter to function

I want to decorate my test to patch specific function. I'm not interested in getting the mock object as a parameter to the function. In this example, I want to be able to omit mock_my_foo parameter:

def my_foo(self):
    print "My side_effect"

class SampleTest(TestCase):
   @patch('some_module.foo', side_effect=my_foo)
   def test_something(self, mock_my_foo):
      pass

I'm using python 2.7

like image 206
eldad levy Avatar asked Oct 15 '25 18:10

eldad levy


1 Answers

I was able to do it with specifying the new param of patch and assigning it a mock object that had the side_effect already set:

def my_foo(self):
    print "My side_effect"

class SampleTest(TestCase):
    @patch('some_module.foo', new=Mock(side_effect=my_foo))
    def test_something(self):
        pass
like image 177
eldad levy Avatar answered Oct 17 '25 07:10

eldad levy