【发布时间】:2017-11-08 19:27:14
【问题描述】:
我对 Angular 还是很陌生,并且一直在构建一个有趣的应用程序来进行一些练习。
目前,我正在尝试集成适当的单元测试,但遇到了一些奇怪的问题。
我已经使用 angular-cli 构建了这个应用程序,并修改了生成的单元测试,以开始充实更好的代码覆盖率以进行测试。
但是,当运行 ng test 时,chrome 浏览器会显示我的应用程序 ui 中的一些 html 元素,这些元素覆盖在业力报告的顶部。
我有一种强烈的感觉,这很可能是因为我做错了什么。在这种情况下,我的 google-fu 帮助不大。 karma 是否应该尝试渲染 UI?
我将测试减少到纯粹是针对我的主 app.component 的测试,即使这样,部分 ui 也会尝试呈现。在下面的代码中,如果我删除了对 PlayerService 的引用,那么 UI 元素将不再呈现叠加在 Karma 输出之上,但当然测试会失败。
app.component.spec.ts
import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { NavigationComponent } from './navigation/navigation.component';
import { PlayerService } from './players/shared/player.service';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule
],
declarations: [
AppComponent,
NavigationComponent
],
providers: [ PlayerService ]
}).compileComponents();
}));
it('should create the app', async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
});
这对我来说似乎很奇怪。
完整的源代码可以在这里找到:
https://github.com/joshuahysong/TICompanion/tree/20170606_tests
有什么建议吗?
谢谢!
【问题讨论】:
标签: angular unit-testing typescript karma-jasmine