问题描述
我正在使用 angular-cli 测试框架.
I am using angular-cli testing framework.
在我的组件中,我使用了ng2-slim-loading-bar"节点模块.
inside my component , I have used 'ng2-slim-loading-bar' node module.
submit(){
this._slimLoadingBarService.start(() => {
});
//method operations
}
现在当我测试这个组件时,我已经将 spyOn 这个服务应用为:
Now when I am testing this component, I have applied spyOn this service as :
beforeEach(() => {
let slimLoadingBarService=new SlimLoadingBarService();
demoComponent = new DemoComponent(slimLoadingBarService);
TestBed.configureTestingModule({
declarations: [
DemoComponent
],
providers: [
{ provide: SlimLoadingBarService, useClass: SlimLoadingBarService}
],
imports: [
SharedModule
]
});
});
it('should pass data to servie', () => {
spyOn(slimLoadingBarService,'start').and.callThrough();
//testing code,if I remove the above service from my component, test runs fine
});
但它不起作用.
它抛出以下错误:
spyOn 无法为 start() 找到要监视的对象
spyOn could not find an object to spy upon for start()
推荐答案
使用 let 声明 slimLoadingBarService,您将其范围限制为 beforeEach 回调范围.用 var 声明它,或者更好的是,在正确的 describe() 块之后声明它,并在 beforeEach 回调函数中设置它的内容:
Declaring slimLoadingBarService with let, you are constraining its scope to the beforeEach callback scope. Declare it with var, or better, declare it after the proper describe() block and set its content within beforeEach callback function:
describe("some describe statement" , function(){
let slimLoadingBarService = null;
beforeEach( () => {
slimLoadingBarService=new SlimLoadingBarService();
});
it('should pass data to service', () => {
spyOn(slimLoadingBarService,'start').and.callThrough();
//testing code,if I remove the above service from my component, test runs fine
});
});
这篇关于spyOn 找不到用于监视 start() 的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)