When writing unit tests for my application, I always used decorators @mock.patchand @patch.object. But now, for some unit tests, when I use the decorator, I get the error " TypeError: staticmethod object is not an iterator ."
But with the same code, if I use mock.patch.objector mock.patch.object, everything works fine.
For example, in my test class, I have this method:
@staticmethod
def my_mock():
...do something
When I try to execute unit test
@mock.patch('mypackage.mymodule.my_method', side_effect=my_mock)
def test_something(self, my_method_mocked):
...test something
I get an error message before TypeError: the staticmethod object is not an iterator .
But when I try this way
def test_something(self):
with patch.object(mymodule, "my_method") as mocked_method:
mocked_method.side_effect = self.my_mock
...test something
then everything works fine.
Python , .
? ?
, :
class TestClass(unittest.TestCase):
@staticmethod
def my_mock():
...mock
return service
@mock.patch('mypackage.mymodule.my_method', side_effect=my_mock)
def test_something(self, my_method_mocked):
...test something
def test_something(self):
with patch.object(mymodule, "my_method") as mocked_method:
mocked_method.side_effect = self.my_mock
...test something
TestClass.my_mock. , .