【发布时间】:2021-10-04 16:35:45
【问题描述】:
我有以下课程
export class Init {
constructor(url: URL) {}
getClient(url: URL): Client {
return new Client(url);
}
}
客户端的定义如下
export class Client {
constructor(readonly url: URL) {}
foo(s: String): Promise<void> {}
// many other methods
}
现在我正在尝试这样测试 - 使用 Jasmine
it('foo should be invoked', done => {
const init = new Init('test-url');
spyOn(init.getClient,'foo');
...
}
在间谍定义中我得到了这个错误
Argument of type 'string' is not assignable to parameter of type 'never'
为什么我可以解决这个问题? Init 的 getClient 方法返回一个 Client 对象。间谍不应该能够识别这种类型吗?
我的最终结果应该是这样的
it('foo should be invoked', done => {
const init = new Init('test-url');
spyOn(init.getClient,'foo');
expect(initCommand.getClient.foo).toHaveBeenCalledTimes(1);
}
【问题讨论】:
标签: javascript testing jasmine karma-jasmine spy