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

Easier session scoped mock #110

Closed
wants to merge 2 commits into from
Closed

Easier session scoped mock #110

wants to merge 2 commits into from

Conversation

akeeman
Copy link
Contributor

@akeeman akeeman commented Sep 26, 2019

Mocker() usually mocks requests.Session.send. This results in a global effect.

This pr adds an argument to Mocker that allows to mock a single Session instance in a similar way, without the need to add custom adapters to a session and removing existing ones.

import requests
import requests_mock

my_session = requests.Session()
m = requests_mock.Mocker(session=my_session)
# ...

Use cases include mocking the session of several independent components that call the same end-point and we are only interested in the call order within a single component, but not in relation to each other.

@jamielennox
Copy link
Owner

So when i first wrote the library I expected many more people to be using a session so just had an adapter object. So requests_mock internally creates an adapter and you can mount that on your own session and then use the standard register_uri or .get() .post() on the adapter object.

@jamielennox
Copy link
Owner

This is not to say we shouldn't be able to still use the mocker method because that's more popular, but if you're looking to support your own use case easier this will work.

I'll try and find some time to review this soon. Thanks.

@akeeman
Copy link
Contributor Author

akeeman commented Sep 27, 2019

After some reading in the documentation I found that too. It, however, is more work ("[...], without the need to add custom adapters to a session and removing existing ones.").
So I think it's great that using the adapter directly works. I just think it's easier for everyone to use the Mocker as a one stop shop. That's all.
Thanks in advance for taking a look at it. If you just don't think it's an improvement, feel free to say so.

@clslgrnc
Copy link
Contributor

clslgrnc commented Apr 3, 2020

I am resolving conflicts in another PR: #133.

@jamielennox
Copy link
Owner

Going to take #133 in favor of this as it merges with other changes for this release.

Thank you for the contribution.

@jamielennox jamielennox closed this May 2, 2020
@akeeman akeeman deleted the patch-1 branch May 3, 2020 07:51
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

Successfully merging this pull request may close these issues.

None yet

3 participants