Skip to content
Advertisement

How to mock functions with same name, when called twice?

How do I mock a function which has been called twice in the same file, with different parameters? Here is my code snippet:

code_file.py

JavaScript

code_test.py

JavaScript

First mock call to graphql is succeeding. The second graphql call is not getting mocked. It is trying to contact the actual server and receiving 404. How can I mock both graphql client calls?

Advertisement

Answer

I tried to reproduce your codes, but for me client.graphql got mocked perfectly.

Here are my codes.

Folder structure

JavaScript
JavaScript
JavaScript
JavaScript

Running unittest by using the command

JavaScript

produces the following output

JavaScript

You’ll see that the prints in prefect.Client methods do not get printed.

Original answer

Explanation

From python official documentation

If side_effect is an iterable then each call to the mock will return the next value from the iterable.

When you set side_effect argument with an iterable, the mock will return next value from the iterable. More detail can be found here

Solution

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement