我使用 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,那么你真的可以传递任何东西。
另见: