如何使用Jasmine测试抛出Exception对象的函数

时间:2014-08-07 16:42:22

标签: javascript exception-handling jasmine

使用Jasmine,我想编写一个测试,期望抛出特定类型的异常。

我正在使用Crockford推荐的抛出异常的方法。

以下代码有效。

describe('toThrow', function() {
    it('checks that the expected exception was thrown by the actual', function() {
      var object = {
        doSomething: function() {
          throw {
            name: 'invalid',
            message: 'Number is invalid'
          }
        }
      };
      expect(object.doSomething).toThrow();
    });
});

问题是:如何编写此测试以便检查抛出的异常名称=='无效'?

2 个答案:

答案 0 :(得分:0)

可以使用以下方法检查姓名和信息:

expect(object.doSomething).toThrow({ name: 'invalid', message: 'Number is invalid' });

可以使用自定义匹配器检查名称。改编自内置toThrow

beforeEach(function () {
    jasmine.addMatchers({
        toThrowPartial: function() {
            function equals(thrown, expected) {
                for(var k in expected) {
                    if(thrown[k] !== expected[k]) return false
                }
                return true
            }

            return {
                compare: function(actual, expected) {
                    var result = { pass: false },
                        threw = false,
                        thrown;

                    if (typeof actual != 'function') {
                        throw new Error('Actual is not a Function');
                    }

                    try {
                        actual();
                    } catch (e) {
                        threw = true;
                        thrown = e;
                    }

                    if (!threw) {
                        result.message = 'Expected function to throw an exception.';
                        return result;
                    }

                    if (arguments.length == 1) {
                        result.pass = true;
                        result.message = function() { return 'Expected function not to throw, but it threw ' + JSON.stringify(thrown) + '.'; };

                        return result;
                    }

                    if (equals(thrown, expected)) {
                        result.pass = true;
                        result.message = function() { return 'Expected function not to throw ' + JSON.stringify(expected) + '.'; };
                    } else {
                        result.message = function() { return 'Expected function to throw ' + JSON.stringify(expected) + ', but it threw ' +    JSON.stringify(thrown) + '.'; };
                    }

                    return result;
                }
            };
        }
    });
});

describe('toThrow', function() {
    it('checks that the expected exception was thrown by the actual', function() {
        var object = {
            doSomething: function() {
                throw {
                    name: 'invalid',
                    message: 'Number is invalid'
                }
            }
        };
        expect(object.doSomething).toThrowPartial({ name: 'invalid' });
    });
});

答案 1 :(得分:0)

您只需指定要与之比较的对象:

expect(object.doSomething).toThrow({name: 'invalid', message: 'Number is invalid'});