From b99b0a80723ad3a9b43e9ef36869f521eaec19ec Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Thu, 23 Jun 2011 16:52:47 +0200 Subject: feat(test): toHaveBeenCalledOnce jasmine matcher --- test/testabilityPatch.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'test/testabilityPatch.js') diff --git a/test/testabilityPatch.js b/test/testabilityPatch.js index 606d29f0..314ba6da 100644 --- a/test/testabilityPatch.js +++ b/test/testabilityPatch.js @@ -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; } }); -- cgit v1.2.3