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

I have a function that throws an error that I am testing.Here is the function:parse: function(input)

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");
  });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745200128a4616282.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信