Skip to content

Commit a084030

Browse files
committed
test(helpers): fix error message generation for toHaveBeenCalledOnce[With] matchers
Jasmine 2.4 (maybe earlier) does not support returning an array containing both the "normal" and the negative error messages. It will always concatenate them. Closes angular#14275
1 parent 1cb8d52 commit a084030

File tree

3 files changed

+65
-37
lines changed

3 files changed

+65
-37
lines changed

test/helpers/matchers.js

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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() {

test/jqLiteSpec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ describe('jqLite', function() {
972972

973973

974974
describe('text', function() {
975-
it('should return null on empty', function() {
975+
it('should return `""` on empty', function() {
976976
expect(jqLite().length).toEqual(0);
977977
expect(jqLite().text()).toEqual('');
978978
});
@@ -1040,7 +1040,7 @@ describe('jqLite', function() {
10401040

10411041

10421042
describe('html', function() {
1043-
it('should return null on empty', function() {
1043+
it('should return `undefined` on empty', function() {
10441044
expect(jqLite().length).toEqual(0);
10451045
expect(jqLite().html()).toEqual(undefined);
10461046
});

test/ng/anchorScrollSpec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ describe('$anchorScroll', function() {
8888
return function($window) {
8989
forEach(elmSpy, function(spy, id) {
9090
var count = map[id] || 0;
91+
// TODO(gkalpak): `toHaveBeenCalledTimes()` works correctly with 0 since
92+
// https://github.com/jasmine/jasmine/commit/342f0eb9a38194ecb8559e7df872c72afc0fe52e
93+
// Fix when we upgrade to a version that contains the fix.
9194
if (count > 0) {
9295
expect(spy).toHaveBeenCalledTimes(count);
9396
} else {
@@ -386,6 +389,9 @@ describe('$anchorScroll', function() {
386389

387390
return function($rootScope, $window) {
388391
inject(expectScrollingTo(identifierCountMap));
392+
// TODO(gkalpak): `toHaveBeenCalledTimes()` works correctly with 0 since
393+
// https://github.com/jasmine/jasmine/commit/342f0eb9a38194ecb8559e7df872c72afc0fe52e
394+
// Fix when we upgrade to a version that contains the fix.
389395
if (list.length > 0) {
390396
expect($window.scrollBy).toHaveBeenCalledTimes(list.length);
391397
} else {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy