feat(test): toHaveBeenCalledOnce jasmine matcher

This commit is contained in:
Vojta Jina 2011-06-23 16:52:47 +02:00 committed by Igor Minar
parent 53a4580d95
commit f6bcbb53f0

View file

@ -90,6 +90,28 @@ beforeEach(function(){
return "Expected " + expected + " to match an Error with message " + toJson(messageRegexp);
};
return this.actual.name == 'Error' && messageRegexp.test(this.actual.message);
},
toHaveBeenCalledOnce: function() {
if (arguments.length > 0) {
throw new Error('toHaveBeenCalledOnce does not take arguments, use toHaveBeenCalledWith');
}
if (!jasmine.isSpy(this.actual)) {
throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.');
}
this.message = function() {
var msg = 'Expected spy ' + this.actual.identity + ' to have been called once, but was ',
count = this.actual.callCount;
return [
count == 0 ? msg + 'never called.'
: msg + 'called ' + count + ' times.',
msg.replace('to have', 'not to have') + 'called once.'
];
};
return this.actual.callCount == 1;
}
});