admin管理员组文章数量:1022964
I would like to test the error handling of a method, which schedules work with setTimeout. The error will be thrown in the scheduled part, i.e.:
function sutWithSetTimeout() {
setTimeout(function () { throw new Error("pang"); }, 1);
}
How do I test that an error is thrown and that it has the correct message?
I would like to test the error handling of a method, which schedules work with setTimeout. The error will be thrown in the scheduled part, i.e.:
function sutWithSetTimeout() {
setTimeout(function () { throw new Error("pang"); }, 1);
}
How do I test that an error is thrown and that it has the correct message?
Share Improve this question edited Feb 12, 2013 at 21:18 delixfe asked Aug 29, 2012 at 11:05 delixfedelixfe 2,4911 gold badge23 silver badges35 bronze badges1 Answer
Reset to default 8You need to catch the function in the setTimeout
and call them using expect(function(){fn();}).toThrow(e);
. So you can spy on setTimeout
to get the function:
spyOn(window, 'setTimeout');
sutWithSetTimeout();
var fn = window.setTimeout.mostRecentCall.args[0];
expect(fn).toThrow('pang');
I would like to test the error handling of a method, which schedules work with setTimeout. The error will be thrown in the scheduled part, i.e.:
function sutWithSetTimeout() {
setTimeout(function () { throw new Error("pang"); }, 1);
}
How do I test that an error is thrown and that it has the correct message?
I would like to test the error handling of a method, which schedules work with setTimeout. The error will be thrown in the scheduled part, i.e.:
function sutWithSetTimeout() {
setTimeout(function () { throw new Error("pang"); }, 1);
}
How do I test that an error is thrown and that it has the correct message?
Share Improve this question edited Feb 12, 2013 at 21:18 delixfe asked Aug 29, 2012 at 11:05 delixfedelixfe 2,4911 gold badge23 silver badges35 bronze badges1 Answer
Reset to default 8You need to catch the function in the setTimeout
and call them using expect(function(){fn();}).toThrow(e);
. So you can spy on setTimeout
to get the function:
spyOn(window, 'setTimeout');
sutWithSetTimeout();
var fn = window.setTimeout.mostRecentCall.args[0];
expect(fn).toThrow('pang');
本文标签: javascriptJasmine test a setTimeout function throws an errorStack Overflow
版权声明:本文标题:javascript - Jasmine: test a setTimeout function throws an error - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745510623a2153809.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论