加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

单元测试 – TestBed configureTestingModule定义一个模块中所有

发布时间:2020-12-17 07:15:51 所属栏目:安全 来源:网络整理
导读:是否有一种最佳的方法来定义所有规范所共有的导入,声明,提供者(即一个地方定义的所有规范所共有的模块,就像我们在@NgModule中所做的那样),就像我们在@NgModule中为应用程序单元测试所做的那样. 注意:在beforeEach中调用configureTestingModule,以便TestBed
是否有一种最佳的方法来定义所有规范所共有的导入,声明,提供者(即一个地方定义的所有规范所共有的模块,就像我们在@NgModule中所做的那样),就像我们在@NgModule中为应用程序单元测试所做的那样.

注意:在beforeEach中调用configureTestingModule,以便TestBed可以在每次测试运行之前将自身重置为基本状态.就像文件一样

在我的一个测试规范中,我必须加载相同的模块组件和指令..etc,这也被其他一些规范使用.

describe('Component: Login',() => {
let loginFixture,loginComponent,loginComponentElement,loginComponentDebugElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
  imports: [FormsModule,ReactiveFormsModule,MaterialRootModule,ModalModule,DatepickerModule,DropdownModule,AccordionModule],//--> here we load n number of mudoles 
  declarations: [LoginComponent,LoginHeaderComponent,LoginColumnComponent,LoginColumnContentComponent,LoginStatusLaneComponent,LoginSettingsComponent,LoginLaneComponent,SortableDirective,WindowHeightDirective,ConfirmDirective,ConfirmPopoverComponent,ConfirmationDialogComponent,ConfirmationDialogDirective],//--> here we load n number of components directive and piper          
  providers: [LoginComponent,MockBackend,BaseRequestOptions,ComponentLoaderFactory,ConfirmOptions,{
      provide: Http,useFactory: (backend,options) => new Http(backend,options),deps: [MockBackend,BaseRequestOptions]
    },{provide: AuthService,useClass: MockAuthService},{provide: AppContextService,useClass: MockAppContextService},{provide: NotificationsService,useClass: MockNotificationsService},{provide: PositioningService}]       //--> here we load n number of services    
}).compileComponents();
loginFixture = TestBed.createComponent(LoginComponent);
loginComponent = loginFixture.componentInstance; 
loginComponentElement = LoginFixture.nativeElement;
loginComponentDebugElement = LoginFixture.debugElement;
}));

 it('should have a defined component',() => {
expect(LoginComponent).toBeDefined();
});
});

注意:Git Angular issue TestBed.configureTestingModule Performance Issue

在运行所有spec.ts文件并为每个规范注入相应的依赖项之前,是否有任何模式可以使它像加载所有这些模块组件等一样常见.任何帮助都会很棒.

解决方法

你的回答是不完整的,你已经开始期待了.
但我希望它能帮到你一点点.

路由器stubs.ts

//----------Default Import------------
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
...snip...
export const test_imports = [
               BrowserModule,FormsModule,HttpModule,BrowserAnimationsModule
             ];

每个test.spec.ts

import { test_imports }   from '../testing/router-stubs';
..snip..
      imports: [test_imports],

但是提供者/声明不能以相同的方式使用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读