如何重用Angular测试文件中的所有导入 [英] How to reuse all imports in Angular test files

查看:123
本文介绍了如何重用Angular测试文件中的所有导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个简单的模块 AppModule ,其中包含许多导入,声明和提供程序.现在,我想为位于该模块的声明列表中的组件 ListComponent 编写测试. ListComponent 本身使用 AppModule 的许多(但不是每次)导入.我是这样的:

Lets say I have a simple module AppModule which has many imports, declarations and providers. Now I want to write a test for a component ListComponent which is located in this module's declaration list. ListComponent itself uses many, (but not every) import of the AppModule. I do it like this:

import { TestBed } from '@angular/core/testing';
// +same copy-pasted list of imports from `AppModule`

beforeEach(done => {
    TestBed.configureTestingModule({
        imports: [
            // +same copy-pasted list of imports from `AppModule`
        ],
        declarations: [
            // +same copy-pasted list of declarations from `AppModule`
        ],
        providers: [
            {
                provide: Http,
                useClass: HttpMock,
            },
            {
                provide: Router,
                useClass: RouterMock,
            }
            // +same copy-pasted list of providers from `AppModule`
        ]
    });

它可以工作,但是肯定是不正确的方法.我不想复制粘贴太多.也许我可以通过一些方便的方法来重用AppModule?伪代码如下:

It works, but surely it is an incorrect approach. I do not want to copy-paste so much. Maybe I can reuse the AppModule in some convenient approach? Pseudocode would be like:

let appModule = new AppModule();

beforeEach(done => {
    TestBed.configureTestingModule({
        imports: appModule.imports,
        declarations: appModule.declarations,
        providers: [...appModule.providers,
            {
                provide: Http,
                useClass: HttpMock,
            },
            {
                provide: Router,
                useClass: RouterMock,
            }
        ]
    });

但是我只是不知道/找不到这种方法的语法:(

But I just do not know/cannot find the syntax for such approach :(

推荐答案

您可以创建可重复使用的const,该const包含来自所需模块的commom导入和提供程序.

You can create reusable const that contains the commom imports, providers from the modules you want.

例如,在app.providers.ts文件中,您可以让提供者像这样:

for example in a app.providers.ts file you can have your providers like this:

import service1 from '.path/service/service1';
import service2 from '.path/service/service2';

export const providers = [service1, service2 ];

,然后在app.imports.ts

and for your imports in a app.imports.ts

import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { Module1} from ''.path/service/module1';

export const imports= [
    BrowserModule,
    AppRoutingModule,
    Module1
],

,以及在您要使用的app.module.ts和任何其他模块上使用相同的导入和提供程序:

and on your app.module.ts and any other module you wanna use the same imports and providers you can do:

import { providers } from './app.providers';
import { imports } from './app.imports';
@NgModule({
    declarations: [AppComponent],
    imports: imports,
    providers: providers,
    bootstrap: [AppComponent]
})

您还可以使用价差运算符将唯一的导入添加到特定模块上的这些共享导入中.

You can also use the spread operator to add your unique imports to these shared imports on a specific module.

这篇关于如何重用Angular测试文件中的所有导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆