没有收到异步测试用例的 SPEC HAS NO EXPECTATIONS 消息
Not getting SPEC HAS NO EXPECTATIONS message for async test case
我正在创建测试用例来演示 angular 使用异步代码进行测试,我想获得 SPEC HAS NO EXPECTATIONS,我有以下代码:
describe('EmployeeService', () => {
let service: EmployeeService;
beforeEach(() => TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [EmployeeService]
}).compileComponents());
beforeEach(() => {
service = TestBed.get(EmployeeService);
});
it('should be return list of employee names ', () => {
service.getListOfEmpNames().subscribe((data: string[]) => {
expect(data.length).toBeGreaterThan(0);
});
});
});
EmployeeService 中的方法:
getListOfEmpNames(): Observable<string[]>{
return of(['Rohan', 'Ethan', 'Pruthvi']);
}
根据我的理解,当我们在没有 done 或 async 或 fakeasync 的情况下测试异步代码时,它将通过测试,但也会在描述之前显示消息 - SPEC HAS NO EXPECTATIONS(如果有误,请纠正我的理解)。
还想知道是否有任何配置可以帮助添加或抑制此类消息。
我很怀疑 Caramiriel
没有触发订阅。
尝试:
it('should be return list of employee names ', async(done) => {
const data = await service.getListofEmpNames().pipe(take(1)).toPromise();
expect(data.length).toBeGreatherThan(0);
done();
});
我通常不喜欢 subscribes
在我的测试中,因为我不知道它是否被执行过。我等待一个承诺完成(然后我知道它被执行了),在这种情况下,如果 getListOfEmpNames
从来没有 returns 任何东西,测试将停留在解决承诺上,从而给你更好的 "engineering".
我正在创建测试用例来演示 angular 使用异步代码进行测试,我想获得 SPEC HAS NO EXPECTATIONS,我有以下代码:
describe('EmployeeService', () => {
let service: EmployeeService;
beforeEach(() => TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [EmployeeService]
}).compileComponents());
beforeEach(() => {
service = TestBed.get(EmployeeService);
});
it('should be return list of employee names ', () => {
service.getListOfEmpNames().subscribe((data: string[]) => {
expect(data.length).toBeGreaterThan(0);
});
});
});
EmployeeService 中的方法:
getListOfEmpNames(): Observable<string[]>{
return of(['Rohan', 'Ethan', 'Pruthvi']);
}
根据我的理解,当我们在没有 done 或 async 或 fakeasync 的情况下测试异步代码时,它将通过测试,但也会在描述之前显示消息 - SPEC HAS NO EXPECTATIONS(如果有误,请纠正我的理解)。 还想知道是否有任何配置可以帮助添加或抑制此类消息。
我很怀疑 Caramiriel
没有触发订阅。
尝试:
it('should be return list of employee names ', async(done) => {
const data = await service.getListofEmpNames().pipe(take(1)).toPromise();
expect(data.length).toBeGreatherThan(0);
done();
});
我通常不喜欢 subscribes
在我的测试中,因为我不知道它是否被执行过。我等待一个承诺完成(然后我知道它被执行了),在这种情况下,如果 getListOfEmpNames
从来没有 returns 任何东西,测试将停留在解决承诺上,从而给你更好的 "engineering".