我使用 Angular 2 Karma-Jasmine。我有服务,
it("test", () => {
let x:any = function1('value', aService);
expect(x).toEqual("value1");
});
现在AService
有getA()
方法,function1
用过的getA()
方法。我想要模拟AService.getA
方法?
请告诉我最好的嘲笑方式AService
?
我使用 Angular 2 Karma-Jasmine。我有服务,
it("test", () => {
let x:any = function1('value', aService);
expect(x).toEqual("value1");
});
现在AService
有getA()
方法,function1
用过的getA()
方法。我想要模拟AService.getA
方法?
请告诉我最好的嘲笑方式AService
?
iffunction
的签名是接受AService
类型
function1(value, service: AService) {}
那么您需要确保模拟与AService
. 如果AService
只有一种方法getA
,那么你真的只需要做
let mockA = {
getA: () => {
// mock implementation
}
}
如果AService
有更多的方法,getA
而您不想实现它,那么您可以将模拟“强制转换”为 type AService
。
let mock = <AService>{ same as above }
或者,如果function1
参数的类型不是AService
,那么你真的可以传递任何东西。
另见: