我有一个特定的功能,我正在尝试使用 Angular 进行测试。此函数返回一个 promise 。虽然在我的测试中我对 promise 结果设定了期望,但 Jasmine 不会等待 promise 被解决或拒绝。
警告:
ERROR: 'Spec 'test' has no expectations.'
测试函数定义如下:public validate(file: File): Promise<any[]> { return new Promise((resolve, reject) => { // Code } }
测试代码:it( test.description, fakeAsync(() => { // Doesnt wait for the following promise result: importValidator.validate(test.testFile).then( resolveValue => { expect(Array.isArray(resolveValue)).toBe(true); }, onReject => { expect(test.resolve).toBeFalsy(); } ); }) );
如何让 Jasmine 在测试期间等待验证 promise 被解决/拒绝?当我因为预期的事情没有发生而让测试失败时,它实际上会在afterAll
中引发错误。 .
最佳答案
如 https://jasmine.github.io/tutorials/async 所述,有不同的方法可以用 Jasmine 测试异步代码。
一种可能的方法是使用 done
功能。
it('#validate should return array', (done) => {
importValidator.validate(test.testFile)
.then(resolveValue => {
expect(Array.isArray(resolveValue)).toBe(true);
done();
})
.catch(err => fail(err));
);
});
关于angular - 如何让 Jasmine 等待一个 promise 被解决或拒绝?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59732836/