Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for arguments to the toThrow() matcher #29

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions spec/suites/MatchersSpec.js
Expand Up @@ -515,6 +515,26 @@ describe("jasmine.Matchers", function() {
expect(match(throwingFn).not.toThrow(new Error("Other Error"))).toPass(); expect(match(throwingFn).not.toThrow(new Error("Other Error"))).toPass();
}); });
}); });

describe("when function only throws based on provided arguments",function(){
beforeEach(function(){
throwingFn = function(fruit,vegetable) {
if(fruit === "Banana") {
throw new Error("Banana Error");
} else if(vegetable === "Tomato") {
throw new Error("Tomato Error");
}
};
});

it("should match when thrown because of argument",function(){
expect(match(throwingFn).toThrow(new Error("Banana Error"),"Banana")).toPass();
});

it("should match when thrown because of subsequent argument",function() {
expect(match(throwingFn).toThrow(new Error("Tomato Error"),"Apple","Tomato")).toPass();
});
});
}); });


describe("when actual is not a function", function() { describe("when actual is not a function", function() {
Expand Down
4 changes: 3 additions & 1 deletion src/Matchers.js
Expand Up @@ -303,7 +303,9 @@ jasmine.Matchers.prototype.toThrow = function(expected) {
throw new Error('Actual is not a function'); throw new Error('Actual is not a function');
} }
try { try {
this.actual(); var args = [];
for(var i=1;i<arguments.length;i++) { args.push(arguments[i]); }
this.actual.apply(this,args);
} catch (e) { } catch (e) {
exception = e; exception = e;
} }
Expand Down