代码之家  ›  专栏  ›  技术社区  ›  Ricardo Rocha

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

  •  8
  • Ricardo Rocha  · 技术社区  · 6 年前

    让我们举个例子:

    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();
        });
    });
    

    如你所见,我有一个模拟组件( MockListViewerGridComponent )和服务 ListConfigurationsService )配置变量( listDefinition )以及我要测试的夹具和组件。

    我的问题是 performance test memory management :

    1. 描述方法内部实例化的变量将在描述内的所有测试完成后立即销毁?
    2. 我应该声明描述中的所有变量和模拟类/服务吗?
    3. 我应该在 beforeEach beforeAll ?通过这样做,我会有性能改进吗?

    谢谢您!

    2 回复  |  直到 6 年前
        1
  •  3
  •   Kedar9444    6 年前

    我有第3点的答案

    让我来分享我自己的经验 beforeAll .

    我们用的是 beforeEach 在我们的一个应用程序中创建fixture花费了将近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 宾果作为一个团队,我们在应用程序构建过程中浪费了大量时间。

    在某种程度上,我们取代了 在每个之前 具有 之前 在…的帮助下 this article 它起作用了。我们能够将构建时间减少大约80%。

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

    1)是的

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

    providers: [
          { provide: XService, useClass: XServiceStub }
    ]
    
        2
  •  1
  •   Ricardo Rocha    6 年前

    在我的项目中,我总是在单独的文件中创建模拟类和测试变量。在将它们导入我的规范文件之后,我将它们声明给 beforeEach 阻止:

    1. 主要的好处是 IT 块它重置它以前的值引用。
    2. 未使用的变量会从内存中删除,从而提高性能。

    我希望这有帮助