gpt4 book ai didi

angular - 单元测试 : No provider for "InterceptableStoreFactory" even if added to "providers"

转载 作者:太空狗 更新时间:2023-10-29 17:26:35 28 4
gpt4 key购买 nike

我正在我的 Angular 应用程序中进行单元测试,我正在使用 TestBed 方法,

我正在测试组件,所以每个规范文件看起来像这样

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");
});
});

如果依赖项导入不好,这种测试方法会导致整个测试套件失败。

例如:在这个测试套件中,它抛出这个错误:

No provider for InterceptableStoreFactory! It seems to be strange , as i'm importing this service in my providers (last one)

这导致几乎所有测试用例的失败,因为 fixture 导入的验证是“beforeEach”测试用例

正在寻找更好的想法:

  1. “服务无提供者”问题(已加入提供者)

为了

  1. unit testBed 更好的测试方式

最佳答案

您提供了两次 InterceptableStoreFactory。一次是模拟替换,一次是原始。尝试删除其中一个。

它可能会帮助您为所有服务创建一个模块并将其放在“核心”文件夹中。 (参见 Angular Style Guide)

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

关于angular - 单元测试 : No provider for "InterceptableStoreFactory" even if added to "providers",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47017663/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com