【发布时间】:2020-02-15 00:23:34
【问题描述】:
我正在努力提高 TDD 的技能,并且我正在重新学习我可以在 Angular 中学习的所有教程。 但这次尝试以 100% 的代码覆盖率对它们进行单元测试。
我有一个愚蠢的问题,我在the documentation 中找不到答案。 在 Stackoverflow 中也找不到这么简单的问题。
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Observable, interval, Subscription } from 'rxjs/';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit, OnDestroy {
secondes: number;
counterSubscritpion: Subscription;
ngOnInit() {
const counter = interval(1000);
const dummy = 'foo';
this.counterSubscritpion = counter.subscribe(
(value) => {
this.secondes = value;
});
}
ngOnDestroy() {
this.counterSubscritpion.unsubscribe();
}
我的模板当然很简单:
<p> nothing should appear here : {{dummy}} </p>
<p> whereas here should be {{secondes}} secondes </p>
所以阅读文档将帮助我测试secondes 和counterSubscritpion...
但我如何测试 counter 和 dummy 已声明以及它们的值?
因为 Karma 的测试覆盖率报告告诉我应该测试 `interval(1000) 调用
到目前为止,我一直坚持这一点:
let fixture: ComponentFixture<AppComponent>;
let routerOutlet: any;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [AppComponent]
});
fixture = TestBed.createComponent(AppComponent);
});
it ('should declare dummy variable', () => {
const appComp = new AppComponent();
appComp.ngOnInit();
// and so what ???
});
【问题讨论】:
标签: angular unit-testing jasmine karma-runner