0

I have a method like below

service.myMethod(reqBody,true, false, false, (success) => {

    },(failure)=>{

    });

I want to spy on this method and want to mock the success callback and I have tried the following

const service = TestBed.get(Service);
spyOn(service, 'myMethod').and.callFake(function (reqBody, success,error) {
        return success({});
    });

Its throwing success is not a function. Also tried the following

spyOn(service, 'myMethod').and.callFake(function (reqBody,true,false,false, success,error) {
        return success({});
    });

Its showing compilation error.

Swayangjit
  • 1,875
  • 2
  • 13
  • 22

2 Answers2

0

In the following solution boolean1, boolean2, boolean3 will receive the values "true, false, false", but you cannot define the values on the function level.

spyOn(service, 'myMethod').and.callFake(function (reqBody, boolean1, boolean2, boolean3, success, error) {
    return success({});
});
Swayangjit
  • 1,875
  • 2
  • 13
  • 22
0

Spying doent work that way, its going to search for the success(), which obviously is not available in your code. The dependencies don't get loaded here means the service is working as an independent component.

To test the service please write the test for the service seperately, here what you can do is to use .and.returnvalue() or .and.returnvalues() instead this will return what ever you want. Please check its documentation to get a better idea (https://jasmine.github.io/2.0/introduction.html#section-Spies:_%3Ccode%3Eand.returnValue%3C/code%3E)

Else write a public success function in your test file above and declare it as public.