Karma + Jasmine(角度测试):我应该在哪里定义模拟类和测试变量?

Ric*_*cha 8 javascript jasmine typescript karma-runner angular

我们来看下面的例子:

const listDefinition: any = {
    module: "module",
    service: "service",
    listname: "listname"
};

@Component(...)
class MockTreeExpanderComponent extends TreeExpanderComponent {...}

class MockListConfigurationsService extends ListConfigurationsService {...}

describe('ColumnsListConfigurationsComponent Test cases', () => {
    let fixture: ComponentFixture<ColumnsListConfigurationsComponent>;
    let component: ColumnsListConfigurationsComponent;
    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [
                ComponentToTest,
                MockTreeExpanderComponent
            ],
            providers: [
                 { provide: TreeListConfigurationService, useClass: MockTreeListConfigurationService }
            ]
        });
        fixture = TestBed.createComponent(ComponentToTest);
        component = fixture.componentInstance;
        component.listDefinition = listDefinition;
        fixture.detectChanges();
    });
});
Run Code Online (Sandbox Code Playgroud)

如您所见,我有一个Mock组件(MockListViewerGridComponent)和service(ListConfigurationsService),一个配置变量(listDefinition)以及我想要测试的fixture和组件.

我的问题是关于performancetest memory management:

  1. 一旦describe中的所有测试完成,在describe方法中实例化的变量将被销毁?
  2. 我应该在describe中声明所有变量和模拟类/服务吗?
  3. 我应该在beforeEach或创建一个夹具beforeAll?通过这样做,我会有性能提升?

谢谢!

Ked*_*444 4

我对第 3 点有一个答案

让我分享一下我自己的经历beforeAll

我们在我们的一个应用程序中使用了beforeEach夹具创建,该应用程序花费了近 12 分钟来构建整个应用程序。为了each unit of work

我们必须构建应用程序

1st time client side

2nd time on review branch

3rd time release branch

几乎(合并)需要30 min提交unit of work

现在,作为一个团队,这次与宾果游戏多次合作,head count of resources我们仅在应用程序构建过程中浪费了大量时间。

在某些时候,我们在本文的帮助下beforeEach替换了,并且它起作用了。我们能够将构建时间缩短约 80%。beforeAll

第 1 点和第 2 点的简短回答

1) 是的

2)最好创建单独的模拟服务。您可以在 before all 块内的存根的帮助下提供它的对象,并将所有模拟保存在同一个文件夹中。

providers: [
      { provide: XService, useClass: XServiceStub }
]
Run Code Online (Sandbox Code Playgroud)