我已经阅读了存在两种测试可观察性的方法
我正在使用angular,并且我有一个具有两个属性的服务
export class MockService {
action$ = new Subject<any>();
multiply$ = this.action$
.pipe(
map( x => x * 2),
);
emitAction(n): void{
this.action$.next(n);
}
}
当action $发出值时,我如何测试乘法$的值? 在我的spec.ts文件中,我尝试过
import { TestBed, } from '@angular/core/testing';
import { MockService } from './mock.service';
import { TestScheduler, } from 'rxjs/testing';
let testScheduler: TestScheduler;
fdescribe('some test', () => {
let service: MockService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ ],
providers: [MockService],
});
testScheduler = new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected);
});
service = TestBed.get(MockService);
});
it('check multiply$', () => {
testScheduler.run( (helpers) => {
const { expectObservable } = helpers;
const expectedValue = 10;
expectObservable(service.multiply$).toBe('c', {c: expectedValue});
service.emitAction(5);
});
});
});
但是我收到一个错误:期望的$ [0] =未定义为等于Object({框架:0,通知:Notification({种类:'N',值:10,错误:未定义,hasValue:true })})。
如果我更改
action$ = new Subject<any>();
到
action$ = new BehaviorSubject<any>(0);
有效,但我想测试subject()