Jasmine SpyOn same method more than once
Also it's possible to call spy properties on the mocked object directly. The code may look like:
spyOn($location,'search');
$location.search.and.returnValue(null);
// do stuff
$location.search.and.returnValue({ foo: "bar" })
// do other stuff
In case of typescript it may look like:
spyOn($location,'search');
(<jasmine.Spy>$location.search).and.returnValue(null);
// do stuff
(<jasmine.Spy>$location.search).and.returnValue({ foo: "bar" })
// do other stuff
Posting this answer because it may look a bit cleaner and doesn't require additional variables.
You can go about it different ways. If you have time to change the spy implementation during your testcase, you can do this:
var searchSpy = spyOn($location,'search');
searchSpy.and.returnValue(null);
// do stuff
searchSpy.and.returnValue({ foo: "bar" });
// do other stuff
If the calls are triggered by a method in your code, and you cannot change the spy implementation in between, then you can create a function that takes the arguments and responds appropriately:
spyOn($location,'search').and.callFake(function(someParam){
if (someParam) {
return { foo: "bar" };
} else {
return { foo: null };
}
});
Of course you can go crazy with logic in your callFake implementation, but beware, I think in that case it might be a code smell. Anyway, happy coding!