【发布时间】:2019-03-08 20:17:10
【问题描述】:
我必须为 Angular 5 应用程序编写单元测试。为了做到这一点,我使用jasmine + jest(我们在公司使用 jest 而不是 karma 来测试速度)。
为了测试我的组件行为(参见下面的代码),我创建了一个订阅相同Observable 的测试作为被测组件,然后等待 2 秒,希望组件的订阅代码块有足够的时间完成,然后,查找组件的内部更改。
问题是随着测试次数的增加,完成测试所需的时间也会增加。而且我个人认为下面一定有更好的方法来测试这种类型的代码。
- 您将如何处理这种情况?我尝试查看
async,但找不到满足我需求的方法。 - 如何确保我的测试在组件的订阅块之后运行?
- 如何避免等待 2 秒才能开始测试,而只需等待组件的订阅块完成?
提前感谢您的帮助。
- 组件:
import { Component, OnInit, OnDestroy } from '@angular/core';
import { SomeService } from './some.service';
@Component({
selector: 'app-dummy',
templateUrl: './dummy.component.html',
styleUrls: ['./dummy.component.scss']
})
export class DummyComponent implements OnInit, OnDestroy {
isEditable: Boolean;
//...
private aSubscriber;
constructor(private someService: SomeService) {
this.aSubscriber = someService.anObservable$.subscribe(value => {
this.isEditable = value;
});
}
ngOnInit() { }
ngOnDestroy() {
this.aSubscriber.unsubscribe();
}
}
- 服务:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class SomeService {
private aSubject = new Subject<any>();
anObservable$ = this.aSubject.asObservable();
constructor() { }
notify(value) {
this.aSubject.next(value);
}
}
- 规格文件:
import { async, ComponentFixture, TestBed, inject } from '@angular/core/testing';
import { DummyComponent } from './dummy.component';
import { SomeService } from './some.service';
describe('DummyComponent', () => {
let component: DummyComponent;
let fixture: ComponentFixture<DummyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [DummyComponent],
providers: [SomeService]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(DummyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should subscribe to anObservable and set values according to the received one',
inject([SomeService], (service: SomeService) => {
service.anObservable$.subscribe(value => {
setTimeout(() => { }, 2000);
//Test that values are correctly set in the component under test.
expect(component.isEditable).toBeTruthy();
//...
});
service.notify(true);
}));
});
【问题讨论】:
标签: angular typescript asynchronous testing jasmine