扔在摩卡测试里面没有被捡起
Throw inside of mocha test not being picked up
尽管打印了所有记录器消息,但我不明白为什么我的测试失败了:
it('should reject subscription to a invalid path', async () => {
const client = fayeConfig.client;
let subscription: any;
expect(() => {
subscription = client.subscribe('/bad_path', (msg: Object) => {
// do nothing
});
subscription.then(() => {
// 'This should never happen.
subscription.cancel();
}, (error: Object) => {
subscription.cancel();
logger.debug(`my error ${error}`);
throw new Error(error.toString());
}).catch((err: Error) => {
subscription.cancel();
logger.debug('something is fishy '+err);
throw err;
});
}).to.throw();
});
我预计错误会出现。任何帮助表示赞赏。干杯!
问题是 expect
需要一个同步函数,而我提供了一个 Promise
。所以它调用函数然后继续。为了解决这个问题,我不得不将函数调用包装在一个单独的函数中,然后等待它解决。
it('should reject subscription to a invalid path', async () => {
const client = fayeConfig.client;
let subscription: any;
const testPromise = async () => {
subscription = client.subscribe('/bad_path', (msg: Object) => {
// do nothing
});
return subscription.then(() => {
subscription.cancel();
}, (error: any) => {
subscription.cancel();
throw new Error(`This is the correct behaviour : ${error}`);
});
};
let test: any = null;
try {
await testPromise();
} catch (error) {
test = error;
}
expect(test).to.be.not.null;
});
尽管打印了所有记录器消息,但我不明白为什么我的测试失败了:
it('should reject subscription to a invalid path', async () => {
const client = fayeConfig.client;
let subscription: any;
expect(() => {
subscription = client.subscribe('/bad_path', (msg: Object) => {
// do nothing
});
subscription.then(() => {
// 'This should never happen.
subscription.cancel();
}, (error: Object) => {
subscription.cancel();
logger.debug(`my error ${error}`);
throw new Error(error.toString());
}).catch((err: Error) => {
subscription.cancel();
logger.debug('something is fishy '+err);
throw err;
});
}).to.throw();
});
我预计错误会出现。任何帮助表示赞赏。干杯!
问题是 expect
需要一个同步函数,而我提供了一个 Promise
。所以它调用函数然后继续。为了解决这个问题,我不得不将函数调用包装在一个单独的函数中,然后等待它解决。
it('should reject subscription to a invalid path', async () => {
const client = fayeConfig.client;
let subscription: any;
const testPromise = async () => {
subscription = client.subscribe('/bad_path', (msg: Object) => {
// do nothing
});
return subscription.then(() => {
subscription.cancel();
}, (error: any) => {
subscription.cancel();
throw new Error(`This is the correct behaviour : ${error}`);
});
};
let test: any = null;
try {
await testPromise();
} catch (error) {
test = error;
}
expect(test).to.be.not.null;
});