@@ -131,25 +131,38 @@ beforeEach(function() {
131
131
return {
132
132
compare : function ( actual ) {
133
133
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`' ) ;
135
136
}
136
137
137
138
if ( ! jasmine . isSpy ( actual ) ) {
138
139
throw new Error ( 'Expected a spy, but got ' + jasmine . pp ( actual ) + '.' ) ;
139
140
}
140
141
142
+ var count = actual . calls . count ( ) ;
143
+ var pass = count === 1 ;
144
+
141
145
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 ;
149
162
} ;
150
163
151
164
return {
152
- pass : actual . calls . count ( ) == 1 ,
165
+ pass : pass ,
153
166
message : message
154
167
} ;
155
168
}
@@ -158,43 +171,52 @@ beforeEach(function() {
158
171
159
172
toHaveBeenCalledOnceWith : function ( util , customEqualityTesters ) {
160
173
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 ) {
163
180
if ( ! jasmine . isSpy ( actual ) ) {
164
181
throw new Error ( 'Expected a spy, but got ' + jasmine . pp ( actual ) + '.' ) ;
165
182
}
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
+
166
191
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 ' ;
176
194
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.' ;
183
197
} 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
+ }
190
209
}
210
+
211
+ return msg ;
191
212
} ;
213
+
192
214
return {
193
- pass : actual . calls . count ( ) === 1 && util . equals ( actual . calls . argsFor ( 0 ) , expectedArgs ) ,
215
+ pass : pass ,
194
216
message : message
195
217
} ;
196
- }
197
- } ;
218
+ } ;
219
+ }
198
220
} ,
199
221
200
222
toBeOneOf : function ( ) {
0 commit comments