I have a function I'd like to test which calls an external API method twice, using different parameters. I'd like to mock this external API out with a Jasmine spy, and return different things based on the parameters. Is there any way to do this in Jasmine? The best I can come up with is a 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; } }); }); });
and.callFakewas a hack? Looks like a good/best answer to me.