[英]Angular2 mocking injectable service
我使用Angular 2 Karma-Jasmine。 我有AService,
it("test", () => {
let x:any = function1('value', aService);
expect(x).toEqual("value1");
});
现在, AService
具有getA()
方法,而function1
使用了getA()
方法。 我想要模拟AService.getA
方法吗?
请告诉我AService
最佳方法吗?
如果function
的签名要接受AService
类型
function1(value, service: AService) {}
那么您需要确保该模拟与AService
兼容。 如果AService
只有一个方法getA
,那么您实际上只需要做
let mockA = {
getA: () => {
// mock implementation
}
}
如果AService
已经不仅仅是更多的方法getA
,你不希望有来实现这一点,那么你可以在“投”模拟输入AService
。
let mock = <AService>{ same as above }
或者,如果未将function1
参数键入为AService
,那么您实际上可以传递任何内容。
另请参阅 :
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.