81
spec/core/matchers/toHaveBeenCalledTimesSpec.js
Normal file
81
spec/core/matchers/toHaveBeenCalledTimesSpec.js
Normal file
@@ -0,0 +1,81 @@
|
||||
describe("toHaveBeenCalledTimes", function() {
|
||||
it("passes when the actual matches the expected", function() {
|
||||
var matcher = j$.matchers.toHaveBeenCalledTimes(),
|
||||
calledSpy = j$.createSpy('called-spy'),
|
||||
result;
|
||||
calledSpy();
|
||||
|
||||
result = matcher.compare(calledSpy, 1);
|
||||
expect(result.pass).toBe(true);
|
||||
});
|
||||
|
||||
it("fails when expected numbers is not supplied", function(){
|
||||
var matcher = j$.matchers.toHaveBeenCalledTimes(),
|
||||
spy = j$.createSpy('spy'),
|
||||
result;
|
||||
|
||||
spy();
|
||||
expect(function() {
|
||||
matcher.compare(spy);
|
||||
}).toThrowError('Expected times failed is required as an argument.');
|
||||
});
|
||||
|
||||
it("fails when the actual was called less than the expected", function() {
|
||||
var matcher = j$.matchers.toHaveBeenCalledTimes(),
|
||||
uncalledSpy = j$.createSpy('uncalled spy'),
|
||||
result;
|
||||
|
||||
result = matcher.compare(uncalledSpy, 2);
|
||||
expect(result.pass).toBe(false);
|
||||
});
|
||||
|
||||
it("fails when the actual was called more than expected", function() {
|
||||
var matcher = j$.matchers.toHaveBeenCalledTimes(),
|
||||
uncalledSpy = j$.createSpy('uncalled spy'),
|
||||
result;
|
||||
|
||||
uncalledSpy();
|
||||
uncalledSpy();
|
||||
|
||||
result = matcher.compare(uncalledSpy, 1);
|
||||
expect(result.pass).toBe(false);
|
||||
});
|
||||
|
||||
it("throws an exception when the actual is not a spy", function() {
|
||||
var matcher = j$.matchers.toHaveBeenCalledTimes(),
|
||||
fn = function() {};
|
||||
|
||||
expect(function() {
|
||||
matcher.compare(fn);
|
||||
}).toThrowError("Expected a spy, but got Function.");
|
||||
});
|
||||
|
||||
it("has a custom message on failure that tells it was called only once", function() {
|
||||
var matcher = j$.matchers.toHaveBeenCalledTimes(),
|
||||
spy = j$.createSpy('sample-spy'),
|
||||
result;
|
||||
spy();
|
||||
spy();
|
||||
spy();
|
||||
spy();
|
||||
|
||||
result = matcher.compare(spy, 1);
|
||||
|
||||
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 = j$.matchers.toHaveBeenCalledTimes(),
|
||||
spy = j$.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.');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,6 +14,7 @@ getJasmineRequireObj().requireMatchers = function(jRequire, j$) {
|
||||
'toEqual',
|
||||
'toHaveBeenCalled',
|
||||
'toHaveBeenCalledWith',
|
||||
'toHaveBeenCalledTimes',
|
||||
'toMatch',
|
||||
'toThrow',
|
||||
'toThrowError'
|
||||
|
||||
30
src/core/matchers/toHaveBeenCalledTimes..js
Normal file
30
src/core/matchers/toHaveBeenCalledTimes..js
Normal file
@@ -0,0 +1,30 @@
|
||||
getJasmineRequireObj().toHaveBeenCalledTimes = function(j$) {
|
||||
|
||||
function toHaveBeenCalledTimes() {
|
||||
return {
|
||||
compare: function(actual, expected) {
|
||||
if (!j$.isSpy(actual)) {
|
||||
throw new Error('Expected a spy, but got ' + j$.pp(actual) + '.');
|
||||
}
|
||||
|
||||
var args = Array.prototype.slice.call(arguments, 0),
|
||||
result = { pass: false };
|
||||
|
||||
if(!expected){
|
||||
throw new Error('Expected times failed is required as an argument.');
|
||||
}
|
||||
|
||||
actual = args[0];
|
||||
var calls = actual.calls.count();
|
||||
var timesMessage = expected === 1 ? 'once' : expected + ' times';
|
||||
result.pass = calls === expected;
|
||||
result.message = result.pass ?
|
||||
'Expected spy ' + actual.and.identity() + ' not to have been called ' + timesMessage + '. It was called ' + calls + ' times.' :
|
||||
'Expected spy ' + actual.and.identity() + ' to have been called ' + timesMessage + '. It was called ' + calls + ' times.';
|
||||
return result;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return toHaveBeenCalledTimes;
|
||||
};
|
||||
Reference in New Issue
Block a user