gpt4 book ai didi

Angular 7 测试错误- rxjs asyncObject 错误 : Timeout - Async callback was not invoked within 5000ms

转载 作者:行者123 更新时间:2023-11-28 21:37:39 33 4
gpt4 key购买 nike

我收到以下错误:

   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();
}
);

另一个同样错误的例子——当我想测试一个示例类已经订阅时,这个类是不可注入(inject)的

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) => {

});

});
});

最佳答案

您在嵌套的 beforeEach 中创建了一个新主题,因此作为 next 传递的值永远不会到达 mock。它包含对包装为可观察对象的旧主题的引用。我猜您在创建新主题后需要重新创建模拟。

关于 Angular 7 测试错误- rxjs asyncObject 错误 : Timeout - Async callback was not invoked within 5000ms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56554693/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com