gpt4 book ai didi

javascript - Karma + Jasmine( Angular Testing ): Where should I define mock classes and test variables?

转载 作者:数据小太阳 更新时间:2023-10-29 04:16:31 26 4
gpt4 key购买 nike

让我们来看下面的例子:

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) 和我要测试的夹具和组件。

我的问题是关于性能测试内存管理:

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

谢谢!

最佳答案

我有第 3 点的答案

让我分享一下我使用 beforeAll 的经验。

我们在我们的一个应用程序中使用了 beforeEach fixture 创建,这花费了将近 12 分钟来构建整个应用程序。对于每个工作单元

我们必须构建应用程序

第一次客户端

第二次审查分支

第三次发布分支

这花费了将近 30 分钟(总计)来提交工作单元

现在随着资源人数的增加,宾果游戏作为一个团队,我们只在应用程序构建过程中浪费了很多时间。

在某些时候,我们在 this article 的帮助下将 beforeEach 替换为 beforeAll ,它奏效了。我们能够将构建时间减少大约 80%。

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

1) 是

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

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

关于javascript - Karma + Jasmine( Angular Testing ): Where should I define mock classes and test variables?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53760985/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com