我的服务中有一个 setter/getter
export class fooService {
set foo(value: number): void {
this._privateFoo = value;
}
get foo(): number {
return this._privateFoo;
}
现在我使用 ts-mockito 模拟了服务,并想检查是否调用了 setter。我尝试通过实例上的 getter 检查变量,但这并没有返回我的值。
组件(FooComponent):
ngOnInit() {
this.fooService.foo = 2;
console.log('Foo Service is now:', this.fooService.foo);
单元测试:
mockFooService = mock(FooService);
fooService = instance(mockFooService);
...
TestBed.configureTestingModule({
declarations: [ FooComponent ],
providers: [
{ provide: FooService: useValue: fooService }
...
component.ngOnInit();
expect(fooService.foo).toEqual(2);
测试打印Foo Service is now: 2
但expect
失败并带有Expected null to equal 2
.