I have a function that I would like to test that calls the external API twice using different parameters. I would like to make fun of this external API with a Jasmine spy and return different things based on parameters. Is there any way to do this in Jasmine? The best I can come up with is hack using andCallFake:
var functionToTest = function() { var userName = externalApi.get('abc'); var userId = externalApi.get('123'); }; describe('my fn', function() { it('gets user name and ID', function() { spyOn(externalApi, 'get').andCallFake(function(myParam) { if (myParam == 'abc') { return 'Jane'; } else if (myParam == '123') { return 98765; } }); }); });
javascript unit-testing jasmine
Jmr Apr 24 '13 at 17:22 2013-04-24 17:22
source share