我遇到以下错误:
Error: Timeout - Async callback was not invoked within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)
at <Jasmine>
如果在每次两个测试都无法正常工作之前不重新初始化resourceSubject $(我的意思是说测试将不会覆盖所有订阅代码),当我尝试在每个之前创建新的AsyncSubject时,出现了以上错误
我的服务订阅了我需要测试的事件
这是我的测试代码:
let resourceSubject$: AsyncSubject<Setting>;
beforeEach(async () => {
resourceLoaderMock.funToMock.and.returnValue({
observableObj: resourceSubject$.asObservable() ,
loadData(){}
});
......
.....
beforeEach(() => {
subject$ = new AsyncSubject<any>();
resourceSubject$ = new AsyncSubject<Data>(); // this make the issue , if i removed it i could not run both test cases
});
fit('should get data from server', async () => {
// given
// when
service.loadData();
resourceSubject$.next(deafultValues)
resourceSubject$.complete();
// then
const res: Setting = await new Promise((resolve, reject) => {
service.subscribe$.subscribe(value => {
resolve(value);
});
});
expect(deafultValues).toEqual(res);
});
我需要测试的服务功能:
this.resource.observableObj.subscribe(
result => {
// Handle result
this.subscriber$.next(result);
this.subscriber$.complete();
},
error => {
this.subscriber$.next(defaultData);
this.subscriber$.complete();
}
);
另一个具有相同错误的示例-当我要测试示例类已订阅时,该类不可注入
describe('spec', () => {
let httpClientMock: jasmine.SpyObj<HttpClient>;
let loggerMock: jasmine.SpyObj<Logger>;
let resourceClass: ResourceLoader<Setting>;
let subject$: AsyncSubject<any>;
beforeEach(async () => {
httpClientMock = jasmine.createSpyObj('httpClient', ['get']);
loggerMock = jasmine.createSpyObj('Logger', [
'debug',
'error',
'trace',
'info',
'build'
]);
loggerMock.build.and.returnValue(loggerMock);
});
beforeEach(() => {
subject$ = new AsyncSubject<any>();
classTest = new ClassTets(Class, 'api-url.com', loggerMock, httpClientMock);
});
it('should test', async () => {
// given
httpClientMock.get.and.returnValue(subject$);
// when
classTest.loadData()
subject$.next({});
subject$.complete();
// then
const result = await new Promise((resolve, reject) => {
});
});
});
答案 0 :(得分:0)
您在嵌套的beforeEach
中创建了一个新主题,因此作为next
传递的值永远不会到达模拟对象。它引用了以可观察方式包装的旧主题。我猜测您在创建新主题后需要重新创建模拟。