question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

different GET params different MOCK, but still the first one's data returned

See original GitHub issue

Inside one test case I add two responses for GET with different url in terms of GET params.

  1. https://maps.googleapis.com/maps/api/geocode/json?latlng=52.212917394179335,20.983095079636158&key=key

  2. https://maps.googleapis.com/maps/api/geocode/json?latlng=52.20212145989611,21.006183521756945&key=key

what happens?

my test case inside in class overriden by django.test.TestCase looks like:

@responses.activate
def test_address_is_updated_correctly_when_spot_location_changed(self):
    latitude_1, longitude_1 = 52.226297, 20.982749
    url = REVERSE_GEOCODING_URL.format(
        latitude=latitude_1,
        longitude=longitude_1,
        api_key=settings.GOOGLE_MAP_API_KEY
    )
    responses.add(
        responses.GET,
        url,
        body=json.dumps(RESPONSE_GEOCODING_MOCK_2),
        content_type="application/json"
    )
    s1 = SpotFactory(
        name='test',
        location=Point(longitude_1, latitude_1)
    )
    s1.save()
    street_before = s1.address_street
    self.assertEqual(street_before, 'Kolejowa')
    latitude_2, longitude_2 = 52.27904, 20.980366
    url2 = REVERSE_GEOCODING_URL.format(
        latitude=latitude_2,
        longitude=longitude_2,
        api_key=settings.GOOGLE_MAP_API_KEY
    )
    responses.add(
        responses.GET,
        url2,
        body=json.dumps(RESPONSE_GEOCODING_MOCK),
        content_type="application/json"
    )
    # optionally IPDB here and done requests.get(url2) and got responses with RESPONSE_GEOCODING_MOCK_2
    s1.location.coords = (longitude_2, latitude_2)
    s1.save()
    s1 = Spot.objects.get(pk=s1.pk)
    self.assertEqual(s1.address_street, 'Mickiewicza')

firs assert is OK

the second one FAILS

because, the response is from the old one mock.

DEBUG: see the place optionally IPDB here and done requests.get(url2) and got responses with RESPONSE_GEOCODING_MOCK_2

When doing first time request I get (RESPONSE_GEOCODING_MOCK_2):

ipdb> import requests; requests.get(url2).json() {‘status’: ‘OK’, ‘results’: [{‘geometry’: {‘bounds’: {‘southwest’: {‘lat’: 52.2261726, ‘lng’: 20.9823615}, ‘northeast’: {‘lat’: 52.22673450000001, ‘lng’: 20.9833889}}, ‘viewport’: {‘southwest’: {‘lat’: 52.22510456970851, ‘lng’: 20.9815262197085}, ‘northeast’: {‘lat’: 52.22780253029151, ‘lng’: 20.98422418029151}}, ‘location’: {‘lat’: 52.2263065, ‘lng’: 20.9828941}, ‘location_type’: ‘ROOFTOP’}, ‘place_id’: ‘ChIJW6aw25vMHkcRxgIq9iMcxA8’, ‘types’: [‘establishment’, ‘point_of_interest’, ‘premise’], ‘formatted_address’: ‘Kolejowa 47, 01-210 Warszawa, Poland’, ‘address_components’: [{‘types’: [‘street_number’], ‘short_name’: ‘47’, ‘long_name’: ‘47’}, {‘types’: [‘route’], ‘short_name’: ‘Kolejowa’, ‘long_name’: ‘Kolejowa’}, {‘types’: [‘political’, ‘sublocality’, ‘sublocality_level_1’], ‘short_name’: ‘Wola’, ‘long_name’: ‘Wola’}, {‘types’: [‘locality’, ‘political’], ‘short_name’: ‘Warszawa’, ‘long_name’: ‘Warszawa’}, {‘types’: [‘administrative_area_level_2’, ‘political’], ‘short_name’: ‘Warszawa’, ‘long_name’: ‘Warszawa’}, {‘types’: [‘administrative_area_level_1’, ‘political’], ‘short_name’: ‘mazowieckie’, ‘long_name’: ‘mazowieckie’}, {‘types’: [‘country’, ‘political’], ‘short_name’: ‘PL’, ‘long_name’: ‘Poland’}, {‘types’: [‘postal_code’], ‘short_name’: ‘01-210’, ‘long_name’: ‘01-210’}]}]}

when doing it for second time I get (RESPONSE_GEOCODING_MOCK):

ipdb> import requests; requests.get(url2).json() {‘status’: ‘OK’, ‘results’: [{‘geometry’: {‘viewport’: {‘southwest’: {‘lat’: 52.2776733197085, ‘lng’: 20.9792990197085}, ‘northeast’: {‘lat’: 52.2803712802915, ‘lng’: 20.9819969802915}}, ‘location’: {‘lat’: 52.2790223, ‘lng’: 20.980648}, ‘location_type’: ‘ROOFTOP’}, ‘place_id’: ‘ChIJxXoMrOfLHkcRBAFsrjKGcMc’, ‘types’: [‘street_address’], ‘formatted_address’: ‘Mickiewicza 74, Warszawa, Poland’, ‘address_components’: [{‘types’: [‘street_number’], ‘short_name’: ‘74’, ‘long_name’: ‘74’}, {‘types’: [‘route’], ‘short_name’: ‘Mickiewicza’, ‘long_name’: ‘Mickiewicza’}, {‘types’: [‘political’, ‘sublocality’, ‘sublocality_level_1’], ‘short_name’: ‘Żoliborz’, ‘long_name’: ‘Żoliborz’}, {‘types’: [‘locality’, ‘political’], ‘short_name’: ‘Warszawa’, ‘long_name’: ‘Warszawa’}, {‘types’: [‘administrative_area_level_2’, ‘political’], ‘short_name’: ‘Warszawa’, ‘long_name’: ‘Warszawa’}, {‘types’: [‘administrative_area_level_1’, ‘political’], ‘short_name’: ‘mazowieckie’, ‘long_name’: ‘mazowieckie’}, {‘types’: [‘country’, ‘political’], ‘short_name’: ‘PL’, ‘long_name’: ‘Poland’}]}]}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
msaizarcommented, Jul 9, 2018

Make sure you use match_querystring=True if you’re using GET params in your URL.

2reactions
PClmntcommented, Jun 25, 2018

Was there ever any movement on this? Seeing the same issue. Did @andilabs provide a PR with example?

EDIT:

So I spent a little more time investigating and this will only happen when your tests are defined within a class like:

class TestBlah(TestCase):

    @responses.activate
    def test_1(self):
        responses.add(responses.GET, 'http://localhost//api/help/versions',
                        status=200, json=[{u'version': u'8.0'}, {u'version': u'8.1'}])
            
        self.assertEqual(str(get_version()), '730')


    @responses.activate
    def test_2(self):
        responses.add(responses.GET, 'http://localhost//api/help/versions',
                      status=200, json=[{u'version': u'6.0'}, {u'version': u'6.1'}])
        self.assertEqual(str(get_version()), '731')

In the above example the JSON from test_1 is used for test_2 as well therefore the assertion fails.

if I remove the class definition and just run the functions on their own with decorators still attached, then its fine. Nevermind this doesnt work either.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Different return values the first and second time with Moq
I need to get different return values from mock based in different input values ... parameters then those can be used as conditionals...
Read more >
Change a mocks return value based on number of times called
Could I create the mocked function to up a counter each time it is called and return a different value based on that...
Read more >
Mocking different values for the same module using Jest
Learn how to use Jest mocking utilities to setup tests with different mock ... function return "GL" in the first invocation, and "EN"...
Read more >
Stubbing and Mocking in Java with the Spock Testing ...
Learn how to create true Java unit tests by mocking all external dependencies in your unit tests with the Spock testing framework.
Read more >
ES6 Class Mocks - Jest
Jest can be used to mock ES6 classes that are imported into files you want to test.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found