diff --git a/spec/core/matchers/toHaveBeenCalledTimesSpec.js b/spec/core/matchers/toHaveBeenCalledTimesSpec.js index 3acef36e..8ea7e5cf 100644 --- a/spec/core/matchers/toHaveBeenCalledTimesSpec.js +++ b/spec/core/matchers/toHaveBeenCalledTimesSpec.js @@ -1,81 +1,117 @@ -describe("toHaveBeenCalledTimes", function() { - it("passes when the actual matches the expected", function() { - var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), - calledSpy = jasmineUnderTest.createSpy('called-spy'), - result; - calledSpy(); +describe("toHaveBeenCalledTimes", function () { - result = matcher.compare(calledSpy, 1); - expect(result.pass).toBe(true); - }); + it("passes when the actual 0 matches the expected 0 ", function () { + 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(){ - var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), - spy = jasmineUnderTest.createSpy('spy'), - result; + result = matcher.compare(calledSpy, 1); + expect(result.pass).toBeTruthy(); + }); - spy(); - expect(function() { - matcher.compare(spy); - }).toThrowError('Expected times failed is required as an argument.'); - }); + it("fails when expected number is not supplied", function () { + var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), + spy = jasmineUnderTest.createSpy('spy'), + result; - it("fails when the actual was called less than the expected", function() { - var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), - uncalledSpy = jasmineUnderTest.createSpy('uncalled spy'), - result; + spy(); + expect(function () { + matcher.compare(spy); + }).toThrowError('The expected times failed is a required argument and must be a number.'); + }); + + it("fails when expected number is a string", function () { + var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), + spy = jasmineUnderTest.createSpy('spy'), + result; - result = matcher.compare(uncalledSpy, 2); - expect(result.pass).toBe(false); - }); + spy(); + expect(function () { + matcher.compare(spy, "1"); + }).toThrowError('The expected times failed is a required argument and must be a number.'); + }); - it("fails when the actual was called more than expected", function() { - var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), - uncalledSpy = jasmineUnderTest.createSpy('uncalled spy'), - result; + it("fails when the actual was called less than the expected", function () { + var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), + uncalledSpy = jasmineUnderTest.createSpy('uncalled spy'), + result; - uncalledSpy(); - uncalledSpy(); + result = matcher.compare(uncalledSpy, 2); + expect(result.pass).toBeFalsy(); + }); - result = matcher.compare(uncalledSpy, 1); - expect(result.pass).toBe(false); - }); + it("fails when the actual was called more than expected", function () { + var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), + uncalledSpy = jasmineUnderTest.createSpy('uncalled spy'), + result; - it("throws an exception when the actual is not a spy", function() { - var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), - fn = function() {}; + uncalledSpy(); + uncalledSpy(); - expect(function() { - matcher.compare(fn); - }).toThrowError("Expected a spy, but got Function."); - }); + result = matcher.compare(uncalledSpy, 1); + expect(result.pass).toBeFalsy(); + }); - it("has a custom message on failure that tells it was called only once", function() { - var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), - spy = jasmineUnderTest.createSpy('sample-spy'), - result; - spy(); - spy(); - spy(); - spy(); + it("throws an exception when the actual is not a spy", function () { + var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), + fn = function () { }; - result = matcher.compare(spy, 1); + expect(function () { + matcher.compare(fn); + }).toThrowError("Expected a spy, but got Function."); + }); - 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 it was called only once", function () { + var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(), + spy = jasmineUnderTest.createSpy('sample-spy'), + result; + spy(); + spy(); + spy(); + spy(); - 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, 1); - result = matcher.compare(spy, 2); + expect(result.message).toEqual('Expected spy sample-spy to have been called once. It was called ' + 4 + ' times.'); + }); - expect(result.message).toEqual('Expected spy sample-spy to have been called 2 times. 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); + });*/ }); diff --git a/src/core/matchers/toHaveBeenCalledTimes..js b/src/core/matchers/toHaveBeenCalledTimes.js similarity index 87% rename from src/core/matchers/toHaveBeenCalledTimes..js rename to src/core/matchers/toHaveBeenCalledTimes.js index b59cc4f4..597968fd 100644 --- a/src/core/matchers/toHaveBeenCalledTimes..js +++ b/src/core/matchers/toHaveBeenCalledTimes.js @@ -10,8 +10,8 @@ getJasmineRequireObj().toHaveBeenCalledTimes = function(j$) { var args = Array.prototype.slice.call(arguments, 0), result = { pass: false }; - if(!expected){ - throw new Error('Expected times failed is required as an argument.'); + if (!j$.isNumber_(expected)){ + throw new Error('The expected times failed is a required argument and must be a number.'); } actual = args[0]; @@ -27,4 +27,4 @@ getJasmineRequireObj().toHaveBeenCalledTimes = function(j$) { } return toHaveBeenCalledTimes; -}; +}; \ No newline at end of file