1

My question is simple. I want to mock a method ONLY if a certain value is passed to the method. Something like this:

spyOn(myObj, "getUser").and.withArgs('userA').and.returnValue(null);

If the method getUser is invoked with the value 'userA', the method should return null.

Is that possibly with Jasmine? I'm using Karma with jasmine-core": "^2.6.1 and jasmine-jquery": "^2.1.1"

Perimosh
  • 2,304
  • 3
  • 20
  • 38

1 Answers1

2

Try taking advantage of callFake:

spyOn(myObj, 'getUser').and.callFake(arg => {
  if(arg === 'userA') {
    return null;
  } else {
    return 'hello'; // it is up to you what to return if it is not 'userA'
  }
});
AliF50
  • 16,947
  • 1
  • 21
  • 37