【发布时间】:2018-04-10 19:21:27
【问题描述】:
我正在尝试使用在构造函数中调用并包含 observable 的私有方法来测试服务:
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/interval';
@Injectable()
export class SomeService {
private booleanValue: boolean = false;
constructor() {
this.obsMethod();
}
private obsMethod() {
Observable.interval(5000).subscribe(() => {
this.booleanValue = !this.booleanValue;
});
}
public getBooleanValue() {
return this.booleanValue;
}
}
我准备了三个规格。首先使用new 运算符创建的简单服务实例。它有效。其次是TestBed.get() 注入。它也有效。
当我在 beforeEach 中使用 inject 时,规范不起作用。但为什么? fakeAsync 和 inject 同时使用有问题吗?如何同时使用它们?
我在 plunker 上创建了带有服务和三个规格的工作演示。
【问题讨论】:
标签: angular testing jasmine inject