在Angular 11中,Jasmine测试框架中的一些函数被标记为已弃用。如果在测试代码中使用这些函数,会导致警告信息的出现,从而影响测试结果的有效性。
以下是被弃用的函数及其用法示例:
async()
函数用于管理异步测试。它已被替换为waitForAsync()
函数。
原用法:
it('should return the expected value', async() => {
const val = await someFunction();
expect(val).toBe(expectedVal);
});
更新后的用法:
it('should return the expected value', waitForAsync(() => {
someFunction().then(val => {
expect(val).toBe(expectedVal);
});
}));
fakeAsync()
函数用于模拟异步操作。它已被替换为fakeAsync()
函数。
原用法:
it('should return the expected value', fakeAsync(() => {
let val = null;
someFunction().then(res => {
val = res;
});
tick(1000);
expect(val).toBe(expectedVal);
}));
更新后的用法:
it('should return the expected value', fakeAsync(() => {
let val = null;
someFunction().then(res => {
val = res;
});
tick(1000);
expect(val).toBe(expectedVal);
}));
jasmine.createSpy()
函数用于创建可供测试使用的spy函数。它已被替换为jasmine.createSpyObj()
函数。
原用法:
const spyFunc = jasmine.createSpy('mySpy');
更新后的用法:
const spyObj = jasmine.createSpyObj('mySpy', ['spyFunc1', 'spyFunc2']);
通过替换这些函数,就可以避免弃用警告信息的出现