tengo las siguientes clases
export class Init { constructor(url: URL) {} getClient(url: URL): Client { return new Client(url); } }y el cliente se define como
export class Client { constructor(readonly url: URL) {} foo(s: String): Promise<void> {} // many other methods }y ahora estoy tratando de probar esto así - con Jasmine
it('foo should be invoked', done => { const init = new Init('test-url'); spyOn(init.getClient,'foo'); ... }En la definición de espía me sale este error
Argument of type 'string' is not assignable to parameter of type 'never' ¿Por qué puedo resolver esto? El método getClient de Init devuelve un objeto Client . ¿No debería el espía ser capaz de identificar este tipo?
Mi resultado final debería verse así
it('foo should be invoked', done => { const init = new Init('test-url'); spyOn(init.getClient,'foo'); expect(initCommand.getClient.foo).toHaveBeenCalledTimes(1); }Solo puedes spyOn métodos públicos. Yo haría esto:
// mock `getClient` object however you like (some examples below) // Return the object/value right away spyOn(init, 'getClient').and.returnValue({ foo: (s: string) => Promise.resolve(s) }); // call a fake function every time init.getClient is called spyOn(init, 'getClient').and.callFake((url) => return {});