In Jasmine versions 3.0 and above you can use withArgs
describe('my fn', function() {
it('gets user name and ID', function() {
spyOn(externalApi, 'get')
.withArgs('abc').and.returnValue('Jane')
.withArgs('123').and.returnValue(98765);
});
});
For Jasmine versions earlier than 3.0 callFake
is the right way to go, but you can simplify it using an object to hold the return values
describe('my fn', function() {
var params = {
'abc': 'Jane',
'123': 98765
}
it('gets user name and ID', function() {
spyOn(externalApi, 'get').and.callFake(function(myParam) {
return params[myParam]
});
});
});
Depending on the version of Jasmine, the syntax is slightly different:
- 1.3.1:
.andCallFake(fn)
- 2.0:
.and.callFake(fn)
Resources:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…