Show argument diffs in toHaveBeenCalledWith failure messages

* Fixes #1641
This commit is contained in:
Steve Gravrock
2019-09-03 17:12:35 -07:00
parent 19292e4ea4
commit 35d15085e3
3 changed files with 36 additions and 6 deletions

View File

@@ -5323,10 +5323,18 @@ getJasmineRequireObj().toHaveBeenCalledWith = function(j$) {
return ' ' + j$.pp(argsForCall);
});
var diffs = actual.calls.allArgs().map(function(argsForCall, callIx) {
var diffBuilder = new j$.DiffBuilder();
util.equals(argsForCall, expectedArgs, customEqualityTesters, diffBuilder);
return 'Call ' + callIx + ':\n' +
diffBuilder.getMessage().replace(/^/mg, ' ');
});
return 'Expected spy ' + actual.and.identity + ' to have been called with:\n' +
' ' + j$.pp(expectedArgs) + '\n' + '' +
'but actual calls were:\n' +
prettyPrintedCalls.join(',\n') + '.';
prettyPrintedCalls.join(',\n') + '.\n\n' +
diffs.join('\n');
};
}

View File

@@ -43,19 +43,33 @@ describe("toHaveBeenCalledWith", function() {
});
it("fails when the actual was called with different parameters", function() {
var util = {
contains: jasmine.createSpy('delegated-contains').and.returnValue(false)
},
var util = jasmineUnderTest.matchersUtil,
matcher = jasmineUnderTest.matchers.toHaveBeenCalledWith(util),
calledSpy = new jasmineUnderTest.Env().createSpy('called spy'),
result;
calledSpy('a');
calledSpy('c', 'd');
calledSpy('a', 'b', 'c');
result = matcher.compare(calledSpy, 'a', 'b');
expect(result.pass).toBe(false);
expect(result.message()).toEqual("Expected spy called spy to have been called with:\n [ 'a', 'b' ]\nbut actual calls were:\n [ 'a' ],\n [ 'c', 'd' ].");
expect(result.message()).toEqual(
"Expected spy called spy to have been called with:\n" +
" [ 'a', 'b' ]\n" +
"but actual calls were:\n" +
" [ 'a' ],\n" +
" [ 'c', 'd' ],\n" +
" [ 'a', 'b', 'c' ].\n\n" +
"Call 0:\n" +
" Expected $.length = 1 to equal 2.\n" +
" Expected $[1] = undefined to equal 'b'.\n" +
"Call 1:\n" +
" Expected $[0] = 'c' to equal 'a'.\n" +
" Expected $[1] = 'd' to equal 'b'.\n" +
"Call 2:\n" +
" Expected $.length = 3 to equal 2.\n" +
" Unexpected $[2] = 'c' in array.");
});
it("throws an exception when the actual is not a spy", function() {

View File

@@ -45,10 +45,18 @@ getJasmineRequireObj().toHaveBeenCalledWith = function(j$) {
return ' ' + j$.pp(argsForCall);
});
var diffs = actual.calls.allArgs().map(function(argsForCall, callIx) {
var diffBuilder = new j$.DiffBuilder();
util.equals(argsForCall, expectedArgs, customEqualityTesters, diffBuilder);
return 'Call ' + callIx + ':\n' +
diffBuilder.getMessage().replace(/^/mg, ' ');
});
return 'Expected spy ' + actual.and.identity + ' to have been called with:\n' +
' ' + j$.pp(expectedArgs) + '\n' + '' +
'but actual calls were:\n' +
prettyPrintedCalls.join(',\n') + '.';
prettyPrintedCalls.join(',\n') + '.\n\n' +
diffs.join('\n');
};
}