最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is jasmine-expect not validating that an error was thrown? - Stack Overflow

programmeradmin1浏览0评论

I have a function that throws an error that I am testing. Here is the function:

parse: function(input) {
        var results = {};       
        var that = this;
        input.forEach(function(dependency) {
            var split = dependency.split(": ");
            var lib = split[0];
            var depen = split[1];       
            if(depen === undefined) {
                throw new Error('Invalid input. Requires a space after each colon.')
            }
            results[lib] = depen;
        });
        return results;
    }

When I test this function, I hit the error code and want to validate that an error is being thrown. Here is my test code:

var invalidInput = ['Service1: ', 'Service2:stuff'] 
expect(manager.parse(invalidInput)).toThrowError();

But my test fails. Here is my stack trace:

Failures:

  1) dependency.js input should require a space after each colon as specified by requirements
   Message:
     Error: Invalid input. Requires a space after each colon.
   Stacktrace:
     Error: Invalid input. Requires a space after each colon.
    at /Users/name/Development/sight/dependency.js:49:11
    at Array.forEach (native)
    at Object.module.exports.parse (/Users/name/Development/sight/dependency.js:44:9)
    at null.<anonymous> (/Users/name/Development/sight/spec/dependency.spec.js:34:12)

I am using jasmine-expect to test for the error being thrown. What am I doing wrong?

I have a function that throws an error that I am testing. Here is the function:

parse: function(input) {
        var results = {};       
        var that = this;
        input.forEach(function(dependency) {
            var split = dependency.split(": ");
            var lib = split[0];
            var depen = split[1];       
            if(depen === undefined) {
                throw new Error('Invalid input. Requires a space after each colon.')
            }
            results[lib] = depen;
        });
        return results;
    }

When I test this function, I hit the error code and want to validate that an error is being thrown. Here is my test code:

var invalidInput = ['Service1: ', 'Service2:stuff'] 
expect(manager.parse(invalidInput)).toThrowError();

But my test fails. Here is my stack trace:

Failures:

  1) dependency.js input should require a space after each colon as specified by requirements
   Message:
     Error: Invalid input. Requires a space after each colon.
   Stacktrace:
     Error: Invalid input. Requires a space after each colon.
    at /Users/name/Development/sight/dependency.js:49:11
    at Array.forEach (native)
    at Object.module.exports.parse (/Users/name/Development/sight/dependency.js:44:9)
    at null.<anonymous> (/Users/name/Development/sight/spec/dependency.spec.js:34:12)

I am using jasmine-expect to test for the error being thrown. What am I doing wrong?

Share Improve this question asked Apr 9, 2015 at 14:16 jhammjhamm 25.1k42 gold badges110 silver badges188 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You need to pass a function as parameter to expect in conjunction with toThrow or toThrowError.

var invalidInput = ['Service1: ', 'Service2:stuff'] 
expect(function () { manager.parse(invalidInput); }).toThrow();

or

var invalidInput = ['Service1: ', 'Service2:stuff'] 
expect(function () { manager.parse(invalidInput); }).toThrowError(Error);

Seens toThrowError to require arguments. Try to set arguments: it can be an expected error message or type, or both.

expect(manager.parse(invalidInput)).toThrowError('Invalid input. Requires a space after each colon.');

For ignoring error message you may use toThrow. From documentation:

it("The 'toThrow' matcher is for testing if a function throws an exception", function() {
    var foo = function() {
      return 1 + 2;
    };
    var bar = function() {
      return a + 1;
    };

    expect(foo).not.toThrow();
    expect(bar).toThrow();
  });

  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");
  });
});
发布评论

评论列表(0)

  1. 暂无评论