javascript 尝试/捕捉茉莉花
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33737807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Try/catch with jasmine
提问by fcortes
I have a function which tries to parse a parameter as a JSON object. If it failed, it uses a fallback instead.
我有一个函数试图将参数解析为 JSON 对象。如果失败,它会使用回退。
parse-code.js
解析代码.js
function parseCode(code) {
try {
usingJSONFallback(code);
} catch() {
usingStringFallback(code);
}
}
function usingJSONFallback(code) {
JSON.parse(code);
//...more code here
}
function usingStringFallback(code) {
//... more code here
}
main.js
主文件
//Some code...
parseCode('hello world!');
I'm not seeing any issue in this code. However, when I'm trying to add some unit tests (using Jasmine 2.3) for the 'catch' case, Jasmine catches the JSON parsing error by its own and abort the test:
我在这段代码中没有看到任何问题。但是,当我尝试为 'catch' 情况添加一些单元测试(使用 Jasmine 2.3)时,Jasmine 自己捕获 JSON 解析错误并中止测试:
For instance, for a Jasmine test like:
例如,对于 Jasmine 测试,例如:
describe('parseCode', function() {
it('Parses a string', function() {
var code = 'My code without JSON';
expect(parseCode(code)).toThrow();
});
});
or even a test like:
甚至像这样的测试:
describe('usingJSONFallback', function() {
it('Throw an error if there is a string', function() {
var code = 'My code without JSON';
expect(usingJSONFallback(code)).toThrow();
});
});
In both cases, the test fails and returns:
在这两种情况下,测试都失败并返回:
SyntaxError: Unable to parse JSON string
I read about throwing controlled exceptions using throw Error(...)
, but definitively this doesn't fit well for my case. Any suggestions about how to use Jasmine in this case?
我阅读了有关使用 抛出受控异常的信息throw Error(...)
,但最终这并不适合我的情况。关于如何在这种情况下使用 Jasmine 的任何建议?
回答by Juan Mendes
You can't call the function yourself, you have to let Jasmine call it by adding a wrapper function. Another way to explain it is that expect
needs a function passed to it when you are testing for it to throw.
不能自己调用函数,必须通过添加包装函数让Jasmine调用。解释它的另一种方法是,expect
当您测试它是否会抛出时,需要将一个函数传递给它。
describe('parseCode', function() {
it('Parses a string', function() {
var code = 'My code without JSON';
expect(function() { parseCode(code) }).toThrow();
});
});
From their example page, notice that the function is passed in but not called.
从他们的示例页面中,注意该函数被传入但未被调用。
it("The 'toThrowError' matcher is for testing a specific thrown exception", function() {
var foo = function() {
throw new TypeError("foo bar baz");
};
expect(foo).toThrowError("foo bar baz");
expect(foo).toThrowError(/bar/);
expect(foo).toThrowError(TypeError);
expect(foo).toThrowError(TypeError, "foo bar baz");
});
回答by Emil A.
Have you tried wrapping the given fn? This way jasmine will be able to execute it on its own and provide extra code to catch it.
你试过包装给定的 fn 吗?这样 jasmine 将能够自己执行它并提供额外的代码来捕获它。
describe("usingJSONFallback", function() {
it("should throw an error if it's called with a string", function() {
expect(function () {
usingJSONFallback("any string");
}).toThrow();
});
});