python side_effect - mocking behavior of a method
Asked Answered
H

1

12

In the mock, I want a certain function to return a new value in the test. This is how i did it.

Class MyClass:

      my_var = None  

      def foo(self, var1):
          return somevalue

      def bar(self):
          my_var = foo(1)

Class TestClass(unittest.TestCase):
      myClass = MyClass() 

      def _side_effect_foo(var1):
           if condition:
                return new_value

      @patch("MyClass", "foo", side_effect='_side_effect_foo')
      def test_foo(self):
           self.myClass.bar()

This gives me a error:

Can't pass kwargs to a mock we aren't creating.

Am I using the correct format of side_effect?

Thanks for any help!

Hindustan answered 29/3, 2018 at 12:33 Comment(0)
D
17

There are a few issues here.

  1. The arguments you're passing to the @patch decorator aren't quite right. The target should be specified as a single argument:

    @patch('MyClass.foo')
    
  2. Your decorated test function will need to accept an additional argument for the patched target - e.g. mock_foo:

    @patch('MyClass.foo')
    def test_foo(self, mock_foo):
    
  3. You can then set the side effect on mock_foo:

    @patch('MyClass.foo')
    def test_foo(self, mock_foo):
        mock_foo.side_effect = self._side_effect_foo
    
  4. In addition, _side_effect_foo() should be made a proper instance method with self as the first argument:

    def _side_effect_foo(self, var1):
        if condition:
            return new_value
    
  5. Lastly, you should instantiate MyClass either inside the test method itself, or inside the setUp() method:

    def setUp(self):
        self.myClass = MyClass()
    
    @patch('MyClass.foo')
    def test_foo(self, mock_foo):
        mock_foo.side_effect = self._side_effect_foo
        self.myClass.bar()
    
Deflation answered 29/3, 2018 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.