【问题标题】:Karma + Jasmine (Angular Testing): Where should I define mock classes and test variables?Karma + Jasmine(Angular 测试):我应该在哪里定义模拟类和测试变量?
【发布时间】:2019-05-14 14:50:31
【问题描述】:

我们来看下面的例子:

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

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

我的问题是关于performancetest memory management

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

谢谢!

【问题讨论】:

  • 根据我的经验,您必须在 beforeEach 中创建变量和模拟类/服务

标签: javascript angular typescript jasmine karma-runner


【解决方案1】:

我有第 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,作为一个宾果团队,我们只在应用构建过程中浪费了大量时间。

在某个时候,我们在this article 的帮助下将beforeEach 替换为beforeAll,并且成功了。我们能够将构建时间减少约 80%。

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

1) 是的

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

providers: [
      { provide: XService, useClass: XServiceStub }
]

【讨论】:

    【解决方案2】:

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

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

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2015-12-07
      • 1970-01-01
      • 2020-03-21
      • 2018-01-31
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多