角度 – 单元测试:即使添加到“提供者”,也不提供“Interceptab
发布时间:2020-12-17 17:36:43 所属栏目:安全 来源:网络整理
导读:我正在Angular应用程序中进行单元测试,我正在使用TestBed方法, 我正在测试组件,所以每个spec文件都是这样的 import...describe('AppComponent',() = {// Importing dependecies beforeEach(async(() = { TestBed.configureTestingModule({ imports : [Router
我正在Angular应用程序中进行单元测试,我正在使用TestBed方法,
我正在测试组件,所以每个spec文件都是这样的 import... describe('AppComponent',() => { // Importing dependecies beforeEach(async(() => { TestBed.configureTestingModule({ imports : [RouterTestingModule,HttpModule,FormsModule ],declarations: [AppComponent ],providers: [AUTH_PROVIDERS,UserService,SharedclientService,RouteNavigator,JwtHelper,ReloadTokenService,ShopService,EnvVarsService,ProfileService,LocalStorageService,ApiVersionInterceptor,ApiTrackingInterceptor,MonitoringService,{ provide: 'LOCAL_STORAGE_SERVICE_CONFIG',useValue: userConfig },TokenUtilService,HttpInterceptorService,{ provide: InterceptableStoreFactory,useClass: InterceptableStoreFactoryMock },ReloadTokenEventService,InterceptableStoreFactory ] }).compileComponents(); })); // detecting changes every times beforeEach(() => { fixture = TestBed.createComponent(AppComponent); component = fixture.componentInstance; fixture.detectChanges(); }); // Test case 0 (compilation of the component) it('AppComponent is well defined',() => { expect(component).toBeDefined(); }); // Test case 1 it('test',() => { expect("1").toBe("1"); }); }); 如果依赖项导入不好,这种测试方法会导致整个测试套件失败. 例如:在此测试套件中,它会抛出此错误:
这导致几乎所有测试用例的失败,因为夹具导入的验证是“beforeEach”测试用例 我正在寻找更好的想法: >“没有服务提供者”的问题(已经添加到提供者“ 并为 >单元测试更好的测试方法 解决方法
您提供两次InterceptableStoreFactory.一次用模拟替换,一次用原版.尝试删除其中一个.
它可以帮助您为所有服务创建一个模块并将其放在“核心”文件夹中. (见Angular Style Guide) 这样可以更轻松地在测试和开发/生产中提供所有正确的服务,而无需如此重复. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |