Search code examples
angularunit-testingjasminekarma-jasmine

Received a call with arguments [ '', undefined ] but all configured strategies specify other arguments


It's weird. In the following code I have tried to pass different arguments to the Get2 method and none seems to work, even with the defined types. Do you have any idea what may be going on?

ERROR

Spy 'PictureService.get2' received a call with arguments [ '', undefined ] but all configured strategies specify other arguments.

CODE

SPEC

let pictureService: jasmine.SpyObj<PictureService>
    
        beforeEach(waitForAsync(() => {
            pictureService = jasmine.createSpyObj('PictureService', ['Get1', 'Get2']);
            pictureService.get1.and.returnValue(of(value1));
            pictureService.get2.withArgs('string1', 'string2').and.returnValue(of('string3'));
        ......

OR

pictureService.get2.withArgs('string1', null).and.returnValue(of('string3'));

OR

pictureService.get2.withArgs('string1', undefined).and.returnValue(of('string3'));

OR

let val:any
pictureService.get2.withArgs('string1', val).and.returnValue(of('string3'));

TS

this.picture.get2('string1', 'string2').subscribe(
                        response => {
                            if (response) {
                                this.image = 'data:image/jpg;base64,' + response;
                            }
                        },
    .......

Solution

  • This is the first time I am seeing withArgs, I usually use callFake.

    If you are always going to return the same value, you can change withArgs to returnValue.

    pictureService.get2.and.returnValue(of('string3'));
    

    If you would like flexibility with the arguments, you can try using callFake.

    pictureService.get2.withArgs('string1', 'string2').and.callFake((string1, string2) => {
      if (string1 && string2) {
        return of('string3');
      }
      return of('string4');
    });
    

    I find callFake more flexible with the arguments.