@@ -131,25 +131,38 @@ beforeEach(function() {
131131 return {
132132 compare : function ( actual ) {
133133 if ( arguments . length > 1 ) {
134- throw new Error ( 'toHaveBeenCalledOnce does not take arguments, use toHaveBeenCalledWith' ) ;
134+ throw new Error ( '`toHaveBeenCalledOnce` does not take arguments, ' +
135+ 'use `toHaveBeenCalledOnceWith`' ) ;
135136 }
136137
137138 if ( ! jasmine . isSpy ( actual ) ) {
138139 throw new Error ( 'Expected a spy, but got ' + jasmine . pp ( actual ) + '.' ) ;
139140 }
140141
142+ var count = actual . calls . count ( ) ;
143+ var pass = count === 1 ;
144+
141145 var message = function ( ) {
142- var msg = 'Expected spy ' + actual . identity ( ) + ' to have been called once, but was ' ,
143- count = this . actual . calls . count ( ) ;
144- return [
145- count === 0 ? msg + 'never called.' :
146- msg + 'called ' + count + ' times.' ,
147- msg . replace ( 'to have' , 'not to have' ) + 'called once.'
148- ] ;
146+ var msg = 'Expected spy ' + actual . and . identity ( ) + ( pass ? ' not ' : ' ' ) +
147+ 'to have been called once, but ' ;
148+
149+ switch ( count ) {
150+ case 0 :
151+ msg += 'it was never called.' ;
152+ break ;
153+ case 1 :
154+ msg += 'it was called once.' ;
155+ break ;
156+ default :
157+ msg += 'it was called ' + count + ' times.' ;
158+ break ;
159+ }
160+
161+ return msg ;
149162 } ;
150163
151164 return {
152- pass : actual . calls . count ( ) == 1 ,
165+ pass : pass ,
153166 message : message
154167 } ;
155168 }
@@ -158,43 +171,52 @@ beforeEach(function() {
158171
159172 toHaveBeenCalledOnceWith : function ( util , customEqualityTesters ) {
160173 return {
161- compare : function ( actual ) {
162- var expectedArgs = Array . prototype . slice . call ( arguments , 1 ) ;
174+ compare : generateCompare ( false ) ,
175+ negativeCompare : generateCompare ( true )
176+ } ;
177+
178+ function generateCompare ( isNot ) {
179+ return function ( actual ) {
163180 if ( ! jasmine . isSpy ( actual ) ) {
164181 throw new Error ( 'Expected a spy, but got ' + jasmine . pp ( actual ) + '.' ) ;
165182 }
183+
184+ var expectedArgs = Array . prototype . slice . call ( arguments , 1 ) ;
185+ var actualCount = actual . calls . count ( ) ;
186+ var actualArgs = actualCount && actual . calls . argsFor ( 0 ) ;
187+
188+ var pass = ( actualCount === 1 ) && util . equals ( actualArgs , expectedArgs ) ;
189+ if ( isNot ) pass = ! pass ;
190+
166191 var message = function ( ) {
167- if ( actual . calls . count ( ) != 1 ) {
168- if ( actual . calls . count ( ) === 0 ) {
169- return [
170- 'Expected spy ' + actual . identity ( ) + ' to have been called once with ' +
171- jasmine . pp ( expectedArgs ) + ' but it was never called.' ,
172- 'Expected spy ' + actual . identity ( ) + ' not to have been called with ' +
173- jasmine . pp ( expectedArgs ) + ' but it was.'
174- ] ;
175- }
192+ var msg = 'Expected spy ' + actual . and . identity ( ) + ( isNot ? ' not ' : ' ' ) +
193+ 'to have been called once with ' + jasmine . pp ( expectedArgs ) + ', but ' ;
176194
177- return [
178- 'Expected spy ' + actual . identity ( ) + ' to have been called once with ' +
179- jasmine . pp ( expectedArgs ) + ' but it was called ' + actual . calls . count ( ) + ' times.' ,
180- 'Expected spy ' + actual . identity ( ) + ' not to have been called once with ' +
181- jasmine . pp ( expectedArgs ) + ' but it was.'
182- ] ;
195+ if ( isNot ) {
196+ msg += 'it was.' ;
183197 } else {
184- return [
185- 'Expected spy ' + actual . identity ( ) + ' to have been called once with ' +
186- jasmine . pp ( expectedArgs ) + ' but was called with ' + jasmine . pp ( actual . calls . argsFor ( 0 ) ) ,
187- 'Expected spy ' + actual . identity ( ) + ' not to have been called once with ' +
188- jasmine . pp ( expectedArgs ) + ' but was called with ' + jasmine . pp ( actual . calls . argsFor ( 0 ) )
189- ] ;
198+ switch ( actualCount ) {
199+ case 0 :
200+ msg += 'it was never called.' ;
201+ break ;
202+ case 1 :
203+ msg += 'it was called with ' + jasmine . pp ( actualArgs ) + '.' ;
204+ break ;
205+ default :
206+ msg += 'it was called ' + actualCount + ' times.' ;
207+ break ;
208+ }
190209 }
210+
211+ return msg ;
191212 } ;
213+
192214 return {
193- pass : actual . calls . count ( ) === 1 && util . equals ( actual . calls . argsFor ( 0 ) , expectedArgs ) ,
215+ pass : pass ,
194216 message : message
195217 } ;
196- }
197- } ;
218+ } ;
219+ }
198220 } ,
199221
200222 toBeOneOf : function ( ) {
0 commit comments