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

javascript - How to assert for an exception using Jasmine? - Stack Overflow

programmeradmin7浏览0评论

I am trying to write a test for ensuring that an invalid instantiation I am doing produces an exception. The test is as follows:

describe('Dialog Spec', function () {
"use strict";

it("should throw an exception if called without a container element", function () {
    expect(function() {
        new Dialog({});
    }).toThrow(new Exception("InvalidArgumentException", "Expected container element missing"));
  });
});

The Dialog() class:

function Dialog(args) {

    if (undefined === args.containerElement)
        throw new Exception("InvalidArgumentException", "Expected container element missing");

    this.containerElement = args.containerElement;

  }
}

I'm getting the following failure in jasmine.

Expected function to throw Exception InvalidArgumentException: Expected container element missing , but it threw Exception InvalidArgumentException: Expected container element missing

My Exception class:

function Exception(exceptionName, exceptionMessage) {

    var name = exceptionName;
    var message = exceptionMessage;

    this.toString = function () {
        return "Exception " + name + ": "+ message;
    };
}

What am I doing wrong?

I am trying to write a test for ensuring that an invalid instantiation I am doing produces an exception. The test is as follows:

describe('Dialog Spec', function () {
"use strict";

it("should throw an exception if called without a container element", function () {
    expect(function() {
        new Dialog({});
    }).toThrow(new Exception("InvalidArgumentException", "Expected container element missing"));
  });
});

The Dialog() class:

function Dialog(args) {

    if (undefined === args.containerElement)
        throw new Exception("InvalidArgumentException", "Expected container element missing");

    this.containerElement = args.containerElement;

  }
}

I'm getting the following failure in jasmine.

Expected function to throw Exception InvalidArgumentException: Expected container element missing , but it threw Exception InvalidArgumentException: Expected container element missing

My Exception class:

function Exception(exceptionName, exceptionMessage) {

    var name = exceptionName;
    var message = exceptionMessage;

    this.toString = function () {
        return "Exception " + name + ": "+ message;
    };
}

What am I doing wrong?

Share Improve this question edited May 16, 2013 at 6:15 Andreas Köberle 111k58 gold badges280 silver badges307 bronze badges asked May 15, 2013 at 18:09 RajRaj 3,0616 gold badges41 silver badges58 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I would split this into multiple tests.

describe("creating a new `Dialog` without a container element", function() {

    it("should throw an exception", function () {
        expect(function() {
            new Dialog({});
        }).toThrow(new Exception("InvalidArgumentException", "Expected container element missing"));
    });

    describe("the thrown exception", function() {

        it("should give a `InvalidArgumentException: Expected container element missing` message", function () {
            try {
                new Dialog({});
                expect(false).toBe(true); // force the text to fail if an exception isn't thrown.
            }
            catch(e) {
                expect(e.toString()).toEqual("InvalidArgumentException: Expected container element missing");
            }
        });

    });

});

The assertion for exception only works with when used with the Javascript in-built Error class instance. I was using my own defined Exception() class and that was the cause of the issue.

发布评论

评论列表(0)

  1. 暂无评论