我正在我的 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");
});
});
如果依赖项导入不好,这种测试方法会导致整个测试套件失败。
例如:在这个测试套件中,它会抛出这个错误:
没有InterceptableStoreFactory的提供者!这似乎很奇怪,因为我正在我的提供商中导入此服务(最后一个)
这导致几乎所有测试用例的失败,因为夹具导入的验证是“ beforeEach ”测试用例
我正在寻找更好的想法:
- “没有服务提供者”的问题(已添加到提供者中)
并且对于
- 单元测试床更好的测试方式