【发布时间】: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) 以及我想要测试的夹具和组件。
我的问题是关于performance 和test memory management:
- describe 方法内部实例化的变量将在 describe 内部的所有测试完成后立即销毁?
- 我应该在 describe 中声明所有变量和模拟类/服务吗?
- 我应该在
beforeEach或beforeAll中创建一个夹具吗?通过这样做,我的性能会有所提高吗?
谢谢!
【问题讨论】:
-
根据我的经验,您必须在 beforeEach 中创建变量和模拟类/服务
标签: javascript angular typescript jasmine karma-runner