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 badges2 Answers
Reset to default 4I 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.