gpt4 book ai didi

angular - Jasmine 中的 TestBed 是什么

转载 作者:太空狗 更新时间:2023-10-29 18:04:49 27 4
gpt4 key购买 nike

我是 Angular 2 的 Jasmine 新手,在编写测试用例时我经常使用 TestBed 对象并收到错误:请在测试前调用“TestBed.compileComponents”。

如何解决这个错误?

@Component({
moduleId:module.id,
selector: 'my-app',
templateUrl: 'app-component.html',

})

最佳答案

Please call "TestBed.compileComponents" before your test

使用 templateUrl 测试组件时需要此调用

Error: Cannot create the component AppComponent as it was not imported into the testing module!

您需要在每次测试前配置TestBed,添加测试所需的任何组件、模块和服务。这就像从头开始配置常规的 @NgModule,但您只需添加所需的内容即可。

import { async, TestBed } from '@angular/core/testing';

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ AppComponent ],
providers: [],
imports: []
})
.compileComponents();
}));

it('...', () => {
let fixture = TestBed.createComponent(AppComponent);
});

另见

关于angular - Jasmine 中的 TestBed 是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39888701/

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