Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

test(helpers): fix error message generation for toHaveBeenCalledOnce[With] matchers #14275

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 57 additions & 35 deletions test/helpers/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,25 +131,38 @@ beforeEach(function() {
return {
compare: function(actual) {
if (arguments.length > 1) {
throw new Error('toHaveBeenCalledOnce does not take arguments, use toHaveBeenCalledWith');
throw new Error('`toHaveBeenCalledOnce` does not take arguments, ' +
'use `toHaveBeenCalledOnceWith`');
}

if (!jasmine.isSpy(actual)) {
throw new Error('Expected a spy, but got ' + jasmine.pp(actual) + '.');
}

var count = actual.calls.count();
var pass = count === 1;

var message = function() {
var msg = 'Expected spy ' + actual.identity() + ' to have been called once, but was ',
count = this.actual.calls.count();
return [
count === 0 ? msg + 'never called.' :
msg + 'called ' + count + ' times.',
msg.replace('to have', 'not to have') + 'called once.'
];
var msg = 'Expected spy ' + actual.and.identity() + (pass ? ' not ' : ' ') +
'to have been called once, but ';

switch (count) {
case 0:
msg += 'it was never called.';
break;
case 1:
msg += 'it was called once.';
break;
default:
msg += 'it was called ' + count + ' times.';
break;
}

return msg;
};

return {
pass: actual.calls.count() == 1,
pass: pass,
message: message
};
}
Expand All @@ -158,43 +171,52 @@ beforeEach(function() {

toHaveBeenCalledOnceWith: function(util, customEqualityTesters) {
return {
compare: function(actual) {
var expectedArgs = Array.prototype.slice.call(arguments, 1);
compare: generateCompare(false),
negativeCompare: generateCompare(true)
};

function generateCompare(isNot) {
return function(actual) {
if (!jasmine.isSpy(actual)) {
throw new Error('Expected a spy, but got ' + jasmine.pp(actual) + '.');
}

var expectedArgs = Array.prototype.slice.call(arguments, 1);
var actualCount = actual.calls.count();
var actualArgs = actualCount && actual.calls.argsFor(0);

var pass = (actualCount === 1) && util.equals(actualArgs, expectedArgs);
if (isNot) pass = !pass;

var message = function() {
if (actual.calls.count() != 1) {
if (actual.calls.count() === 0) {
return [
'Expected spy ' + actual.identity() + ' to have been called once with ' +
jasmine.pp(expectedArgs) + ' but it was never called.',
'Expected spy ' + actual.identity() + ' not to have been called with ' +
jasmine.pp(expectedArgs) + ' but it was.'
];
}
var msg = 'Expected spy ' + actual.and.identity() + (isNot ? ' not ' : ' ') +
'to have been called once with ' + jasmine.pp(expectedArgs) + ', but ';

return [
'Expected spy ' + actual.identity() + ' to have been called once with ' +
jasmine.pp(expectedArgs) + ' but it was called ' + actual.calls.count() + ' times.',
'Expected spy ' + actual.identity() + ' not to have been called once with ' +
jasmine.pp(expectedArgs) + ' but it was.'
];
if (isNot) {
msg += 'it was.';
} else {
return [
'Expected spy ' + actual.identity() + ' to have been called once with ' +
jasmine.pp(expectedArgs) + ' but was called with ' + jasmine.pp(actual.calls.argsFor(0)),
'Expected spy ' + actual.identity() + ' not to have been called once with ' +
jasmine.pp(expectedArgs) + ' but was called with ' + jasmine.pp(actual.calls.argsFor(0))
];
switch (actualCount) {
case 0:
msg += 'it was never called.';
break;
case 1:
msg += 'it was called with ' + jasmine.pp(actualArgs) + '.';
break;
default:
msg += 'it was called ' + actualCount + ' times.';
break;
}
}

return msg;
};

return {
pass: actual.calls.count() === 1 && util.equals(actual.calls.argsFor(0), expectedArgs),
pass: pass,
message: message
};
}
};
};
}
},

toBeOneOf: function() {
Expand Down
4 changes: 2 additions & 2 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ describe('jqLite', function() {


describe('text', function() {
it('should return null on empty', function() {
it('should return `""` on empty', function() {
expect(jqLite().length).toEqual(0);
expect(jqLite().text()).toEqual('');
});
Expand Down Expand Up @@ -1040,7 +1040,7 @@ describe('jqLite', function() {


describe('html', function() {
it('should return null on empty', function() {
it('should return `undefined` on empty', function() {
expect(jqLite().length).toEqual(0);
expect(jqLite().html()).toEqual(undefined);
});
Expand Down
2 changes: 1 addition & 1 deletion test/modules/no_bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// When runnint the modules test, then the page should not be bootstrapped.
// When running the modules test, then the page should not be bootstrapped.
window.name = "NG_DEFER_BOOTSTRAP!";

6 changes: 6 additions & 0 deletions test/ng/anchorScrollSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ describe('$anchorScroll', function() {
return function($window) {
forEach(elmSpy, function(spy, id) {
var count = map[id] || 0;
// TODO(gkalpak): `toHaveBeenCalledTimes()` works correctly with 0 since
// https://github.com/jasmine/jasmine/commit/342f0eb9a38194ecb8559e7df872c72afc0fe52e
// Fix when we upgrade to a version that contains the fix.
if (count > 0) {
expect(spy).toHaveBeenCalledTimes(count);
} else {
Expand Down Expand Up @@ -386,6 +389,9 @@ describe('$anchorScroll', function() {

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