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

javascript - Error Expectations in Jasmine - Stack Overflow

programmeradmin5浏览0评论

I have the following function which works

function sum ()
{
    var total = 0,
        num = 0,
        numArgs = arguments.length;

    if (numArgs === 0) {
        throw new Error("Arguments Expected");
    }

    for(var c = 0; c < numArgs; c += 1) {
        num = arguments[c];
        if (typeof(num) !== "number") {
            throw new Error("Only number are allowed but found", typeof (num));
        }
        total += num;

    }

    return total;

}


sum(2, "str"); // Error: Only number are allowed but found "string"

The jasmine spec file is the following:

describe("First test; example specification", function () {
    it("should be able to add 1 + 2", function (){
        var add = sum(1, 2);
        expect(add).toEqual(3);
    });
    it("Second Test; should be able to catch the excption 1 +'s'", function (){
        var add = sum(1, "asd");
        expect(add).toThrow(new Error("Only number are allowed but found", typeof("asd")));
    });
});

The fist test works great, for the second one I get a failing test.
How should I handle the expected error in Jasmine?

I have the following function which works

function sum ()
{
    var total = 0,
        num = 0,
        numArgs = arguments.length;

    if (numArgs === 0) {
        throw new Error("Arguments Expected");
    }

    for(var c = 0; c < numArgs; c += 1) {
        num = arguments[c];
        if (typeof(num) !== "number") {
            throw new Error("Only number are allowed but found", typeof (num));
        }
        total += num;

    }

    return total;

}


sum(2, "str"); // Error: Only number are allowed but found "string"

The jasmine spec file is the following:

describe("First test; example specification", function () {
    it("should be able to add 1 + 2", function (){
        var add = sum(1, 2);
        expect(add).toEqual(3);
    });
    it("Second Test; should be able to catch the excption 1 +'s'", function (){
        var add = sum(1, "asd");
        expect(add).toThrow(new Error("Only number are allowed but found", typeof("asd")));
    });
});

The fist test works great, for the second one I get a failing test.
How should I handle the expected error in Jasmine?

Share Improve this question asked Apr 19, 2012 at 17:11 underscore666underscore666 1,7395 gold badges24 silver badges38 bronze badges 2
  • To pass arguments to the function being tested, without using an anonymous function, try Function.bind: stackoverflow./a/13233194/294855 – Danyal Aytekin Commented Nov 5, 2012 at 14:55
  • possible duplicate of How to write a test which expects an Error to be thrown – Factor Mystic Commented Aug 7, 2014 at 14:18
Add a ment  | 

1 Answer 1

Reset to default 16

As discussed in this question, your code does not work because you should pass a function object to expect rather than the result of calling fn()

    it("should be able to catch the excption 1 +'s'", function (){
//        var add = sum(1, "asd");
        expect(function () {
            sum(1, "asd");
        }).toThrow(new Error("Only number are allowed but found", typeof ("asd")));
    });
发布评论

评论列表(0)

  1. 暂无评论