2

我试图对打字稿中的通用方法进行间谍活动,
但无法让 Jasmine 识别它。


我有代码

http: HttpClient <- Not proper code, just showing type.
...
this.http.get<Customer[]>(url);

我想在哪里模拟该get<...>方法。

const httpMock = {} as HttpClient;
spyOn(httpMock, 'get')
   .and.returnValue(of({} as Customer[]));

但是当我运行测试时,我得到

错误::get()方法不存在
用法:spyOn(,)

4

1 回答 1

0

补救措施是在模拟时使用任何泛型。

describe('UserService', () => {
  it('should return proper users', (done) => {

    const returnedUsers = [...];

    const mock = {
      get: (_) => of({returnUsers}),
    } as HttpClient;

    const sut = new UserService(mock);

    //  Act.
    sut.getUser()
        .subsribe(users => {

            //  Assert.
            ...

            done();
        });
  });
});
于 2020-06-22T09:07:39.313 回答