Sinon and fake server / google maps

359 views
Skip to first unread message

Jakub Alkagar

unread,
Feb 11, 2014, 5:26:56 AM2/11/14
to sin...@googlegroups.com
Hi,

I just started with sinon and jasmine tests. Can you advise me what would be the best approach to mock/fake response from google maps api?

My code looks like that:

self.getDistance = function(callback) {
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
        {
            origins: self.origin,
            destinations: self.destination,
            travelMode: google.maps.TravelMode.DRIVING,
            unitSystem: google.maps.UnitSystem.METRIC,
            avoidHighways: false,
            avoidTolls: false
        }, callback);
};


Response from google maps is not via xhr request so I think that fake server will not work (will it?). I found loaded js script file in firebug (single file for single request/response) - is there any way to catch that and replace with mockup made by sinon?

What would be the best approach to test such case?

Thanks for any suggestions!

Jakub.

Dane Stuckel

unread,
Feb 12, 2014, 11:40:38 AM2/12/14
to sin...@googlegroups.com
This may just be me, but I use sandboxes with stubs very heavily for these kinds of situations.  For example:

var sandbox;

beforeEach: function () {
  sandbox = sinon.sandbox.create();
}

afterEach: function () {
  sandbox.restore();
}

it("some test", function () {

  sandbox.stub(service, "getDistanceMatrix");

  etc...

})

Then you can use the other sinon api calls to say that the second argument should be called with whatever parameters you want.  When the test it done, then the service's function is restored back to normal.

Jakub Mrowiec

unread,
Feb 13, 2014, 12:45:16 AM2/13/14
to sin...@googlegroups.com
OK thanks, but I still can't check if second argument was called right after the call of getDistanceMatrix, don't I? getDistanceMatrix loads data with async request and after getting response call my callback.

I can of course work it out with some waits and runs but it will force me to run test with working internet connection so getDistanceMatrix can load real data.

Is there a way to fake this kind of response?


--
Jakub Mrowiec

phone: 505 977 114


--
You received this message because you are subscribed to a topic in the Google Groups "Sinon.JS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sinonjs/u6H-QppFpPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sinonjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Dane Stuckel

unread,
Feb 13, 2014, 3:06:51 PM2/13/14
to sin...@googlegroups.com, ja...@mrowiec.org
What exactly is under test?  You probably shouldn't actually call the service api, because it's not yours.  Instead, you should fake it so that only your own code is under test.


The following code will call the second argument that was provided when someone calls service.getDistanceMatrix:

sandbox.stub(service, "getDistanceMatrix").callsArg(1);

You can provide the callback with parameters by using:

sandbox.stub(service, "getDistanceMatrix").callsArgWith(1, someParam1, someParam2);


Reply all
Reply to author
Forward
0 new messages