Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leveraging the existing functionality for configuring mocks #16

Closed
nirwall opened this issue May 4, 2020 · 2 comments
Closed

leveraging the existing functionality for configuring mocks #16

nirwall opened this issue May 4, 2020 · 2 comments

Comments

@nirwall
Copy link

nirwall commented May 4, 2020

Is there a way to use the functionality for configuring a mock to return specific values (using side_effect as it seems from how mock works) when called with specific parameters?

@Xion
Copy link
Owner

Xion commented May 4, 2020

AFAIK, the built-in Python mocks do not support this. You will probably need to implement such logic inside a custom function and assign it to side_effect, e.g.:

def dispatcher(arg):
    if isinstance(arg, int):
        return int_version(arg)
    if isinstance(arg, str):
        return str_version(arg)
    # etc.

mock = Mock()
mock.side_effect = dispatcher

Note that such mocking is intentionally outside of scope of callee (though it could be part of a bigger library that combines callee with unittest.mock to provide some of the functionality of Java's Mockito, incl. the when-then style of mocking).

@nirwall
Copy link
Author

nirwall commented May 10, 2020

Thanks!

@nirwall nirwall closed this as completed May 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants