'How to 'Ignore' a Method using Python-Mockito?

I am new to unit testing and the mockito library, and am trying to figure out how to 'skip' or 'ignore' a class instantiation within a method call. I have some code that looks like this:

def main(args):
  ...
  ...
  notify = Notify(some_variables)
  ...
  try:
    ...
    ...
  except Exceptions as err:
    notify.alert(some_variables)

I tried mocking this method like:

notice_mock = mock()
when(notify).Notify(some_variables).thenReturn(notice_mock)

However, it still instantiated the class and is throwing errors because of other method calls in that class. Am I misunderstanding the function of mock()? Is there a way to ignore this method?



Solution 1:[1]

That's a question about "where" to mock. The code under test uses Notify(...) within def main. Assume, you may have this in a file foo.py.

During the test you essentially mock notify.Notify(...) (when(<object/container>).<name/member>(...)) but the code under test uses foo.Notify.

You may have a import foo as system_under_test line in the test file or something similar. Then a when(system_under_test).Notify(...) should do the patch.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 herr.kaste