gpt4 book ai didi

javascript - 有没有办法为 Jasmine 单元测试(Angular 4)提供基类?

转载 作者:行者123 更新时间:2023-12-03 03:53:35 24 4
gpt4 key购买 nike

我有几个 .spec.ts 文件,它们都需要以下 beforeEach:

  beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [FormsModule, HttpModule, CovalentHttpModule.forRoot({
interceptors: [{
interceptor: CustomInterceptorService, paths: ['**'],
}],
}),],
declarations: [LoginComponent],
providers: [AuthService, { provide: Router, useClass: class { navigate = jasmine.createSpy('navigate'); } }, LoggingService, CustomInterceptorService],
})
.compileComponents();
}));

有没有办法外包这个 TestBed 配置?现在我必须使用相同的导入和提供程序来调整每个新的测试文件。

我正在寻找类似基本单元测试的东西。 Jasmine 可以吗?

最佳答案

这个案例并不是 Jasmine 特有的。如果需要重用某个函数,可以通过 JavaScript/TypeScript 来完成。

辅助函数:

export const setupFooTestbed = async(() => {
TestBed.configureTestingModule({...})...
});
...
beforeEach(setupFooTestbed);

或者它可以是 TestModuleMetadata 的基类TestBed.configureTestingModule 接受的对象:

export FooTestModuleMetadata implements TestModuleMetadata { ... }
...
beforeEach(async(() => {
TestBed.configureTestingModule(new FooTestModuleMetadata)...
}));

或者可以向 initTestEnvironment method 提供通用测试模块:

Initialize the environment for testing with a compiler factory, a PlatformRef, and an angular module. These are common to every test in the suite.

This may only be called once, to set up the common providers for the current test suite on the current platform. If you absolutely need to change the providers, first use resetTestEnvironment.

关于javascript - 有没有办法为 Jasmine 单元测试(Angular 4)提供基类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45063207/

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