Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了角度 – 单元测试:即使添加到“提供者”,也不提供“InterceptableStoreFactory”大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在Angular应用程序中进行单元测试,我正在使用TESTBed方法,

我正在测试组件,所以每个spec文件是这样的

import...
describe('AppComponent',() => {
// ImporTing dependecies
    beforeEach(async(() => {
        TESTBed.configureTesTingModule({
            imports : [RouterTesTingModule,httpR_847_11845@odule,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)

这样可以更轻松地在测试和开发/生产中提供所有正确的服务,而无需如此重复.

大佬总结

以上是大佬教程为你收集整理的角度 – 单元测试:即使添加到“提供者”,也不提供“InterceptableStoreFactory”全部内容,希望文章能够帮你解决角度 – 单元测试:即使添加到“提供者”,也不提供“InterceptableStoreFactory”所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。