#1015 use isNumber fuction to check for empty expected argument. This will allow 0 to be passed in.

This commit is contained in:
Kevin Logan
2016-02-17 12:03:06 -06:00
parent 4fb5aa14b8
commit e5c744f3dc
2 changed files with 102 additions and 66 deletions

View File

@@ -1,81 +1,117 @@
describe("toHaveBeenCalledTimes", function() { describe("toHaveBeenCalledTimes", function () {
it("passes when the actual matches the expected", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
calledSpy = jasmineUnderTest.createSpy('called-spy'),
result;
calledSpy();
result = matcher.compare(calledSpy, 1); it("passes when the actual 0 matches the expected 0 ", function () {
expect(result.pass).toBe(true); var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
}); calledSpy = jasmineUnderTest.createSpy('called-spy'),
result;
result = matcher.compare(calledSpy, 0);
expect(result.pass).toBeTruthy();
});
it("passes when the actual matches the expected", function () {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
calledSpy = jasmineUnderTest.createSpy('called-spy'),
result;
calledSpy();
it("fails when expected numbers is not supplied", function(){ result = matcher.compare(calledSpy, 1);
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), expect(result.pass).toBeTruthy();
spy = jasmineUnderTest.createSpy('spy'), });
result;
spy(); it("fails when expected number is not supplied", function () {
expect(function() { var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
matcher.compare(spy); spy = jasmineUnderTest.createSpy('spy'),
}).toThrowError('Expected times failed is required as an argument.'); result;
});
it("fails when the actual was called less than the expected", function() { spy();
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), expect(function () {
uncalledSpy = jasmineUnderTest.createSpy('uncalled spy'), matcher.compare(spy);
result; }).toThrowError('The expected times failed is a required argument and must be a number.');
});
result = matcher.compare(uncalledSpy, 2); it("fails when expected number is a string", function () {
expect(result.pass).toBe(false); var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
}); spy = jasmineUnderTest.createSpy('spy'),
result;
it("fails when the actual was called more than expected", function() { spy();
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), expect(function () {
uncalledSpy = jasmineUnderTest.createSpy('uncalled spy'), matcher.compare(spy, "1");
result; }).toThrowError('The expected times failed is a required argument and must be a number.');
});
uncalledSpy(); it("fails when the actual was called less than the expected", function () {
uncalledSpy(); var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
uncalledSpy = jasmineUnderTest.createSpy('uncalled spy'),
result;
result = matcher.compare(uncalledSpy, 1); result = matcher.compare(uncalledSpy, 2);
expect(result.pass).toBe(false); expect(result.pass).toBeFalsy();
}); });
it("throws an exception when the actual is not a spy", function() { it("fails when the actual was called more than expected", function () {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
fn = function() {}; uncalledSpy = jasmineUnderTest.createSpy('uncalled spy'),
result;
expect(function() { uncalledSpy();
matcher.compare(fn); uncalledSpy();
}).toThrowError("Expected a spy, but got Function.");
});
it("has a custom message on failure that tells it was called only once", function() { result = matcher.compare(uncalledSpy, 1);
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), expect(result.pass).toBeFalsy();
spy = jasmineUnderTest.createSpy('sample-spy'), });
result;
spy();
spy();
spy();
spy();
result = matcher.compare(spy, 1); it("throws an exception when the actual is not a spy", function () {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
fn = function () { };
expect(result.message).toEqual('Expected spy sample-spy to have been called once. It was called ' + 4 + ' times.'); expect(function () {
}); matcher.compare(fn);
}).toThrowError("Expected a spy, but got Function.");
});
it("has a custom message on failure that tells how many times it was called", function() { it("has a custom message on failure that tells it was called only once", function () {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
spy = jasmineUnderTest.createSpy('sample-spy'), spy = jasmineUnderTest.createSpy('sample-spy'),
result; result;
spy(); spy();
spy(); spy();
spy(); spy();
spy(); spy();
result = matcher.compare(spy, 2); result = matcher.compare(spy, 1);
expect(result.message).toEqual('Expected spy sample-spy to have been called 2 times. It was called ' + 4 + ' times.'); expect(result.message).toEqual('Expected spy sample-spy to have been called once. It was called ' + 4 + ' times.');
}); });
it("has a custom message on failure that tells how many times it was called", function () {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
spy = jasmineUnderTest.createSpy('sample-spy'),
result;
spy();
spy();
spy();
spy();
result = matcher.compare(spy, 2);
expect(result.message).toEqual('Expected spy sample-spy to have been called 2 times. It was called ' + 4 + ' times.');
});
/*
it("should work when chained to expect", function(){
// test for the way we'll use it in real code
var foo = {
func: function(){}
};
spyOn(foo, 'func');
expect(foo.func).toHaveBeenCalledTimes(0);
foo.func();
expect(foo.func).toHaveBeenCalledTimes(1);
foo.func();
foo.func();
foo.func();
expect(foo.func).toHaveBeenCalledTimes(4);
foo.func.calls.reset();
expect(foo.func).toHaveBeenCalledTimes(0);
});*/
}); });

View File

@@ -10,8 +10,8 @@ getJasmineRequireObj().toHaveBeenCalledTimes = function(j$) {
var args = Array.prototype.slice.call(arguments, 0), var args = Array.prototype.slice.call(arguments, 0),
result = { pass: false }; result = { pass: false };
if(!expected){ if (!j$.isNumber_(expected)){
throw new Error('Expected times failed is required as an argument.'); throw new Error('The expected times failed is a required argument and must be a number.');
} }
actual = args[0]; actual = args[0];