From bcc28d7063971cd04cad89a1bcee2c96726abe97 Mon Sep 17 00:00:00 2001 From: Maksym Kobieliev Date: Thu, 2 Apr 2020 21:31:17 +0300 Subject: [PATCH] Output a diff if there was only one call, but with wrong parameters --- .../matchers/toHaveBeenCalledOnceWithSpec.js | 2 +- src/core/matchers/toHaveBeenCalledOnceWith.js | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/spec/core/matchers/toHaveBeenCalledOnceWithSpec.js b/spec/core/matchers/toHaveBeenCalledOnceWithSpec.js index b3678bfb..97fdaf9c 100644 --- a/spec/core/matchers/toHaveBeenCalledOnceWithSpec.js +++ b/spec/core/matchers/toHaveBeenCalledOnceWithSpec.js @@ -49,7 +49,7 @@ describe("toHaveBeenCalledOnceWith", function () { result = matcher.compare(calledSpy, 'a', 'b'); expect(result.pass).toBe(false); - expect(result.message).toEqual("Expected spy called-spy to have been called only once, and with given args:\n [ 'a', 'b' ]\nBut the actual calls were:\n [ 'a', 'c' ].\n\n"); + expect(result.message).toEqual("Expected spy called-spy to have been called only once, and with given args:\n [ 'a', 'b' ]\nBut the actual call was:\n [ 'a', 'c' ].\nExpected $[1] = 'c' to equal 'b'.\n\n"); }); it("fails when the actual was called multiple times with expected parameters", function () { diff --git a/src/core/matchers/toHaveBeenCalledOnceWith.js b/src/core/matchers/toHaveBeenCalledOnceWith.js index ccd707c7..035848b0 100644 --- a/src/core/matchers/toHaveBeenCalledOnceWith.js +++ b/src/core/matchers/toHaveBeenCalledOnceWith.js @@ -36,10 +36,23 @@ getJasmineRequireObj().toHaveBeenCalledOnceWith = function (j$) { }; } + function getDiffs() { + return actual.calls.allArgs().map(function (argsForCall, callIx) { + var diffBuilder = new j$.DiffBuilder(); + util.equals(argsForCall, expectedArgs, customEqualityTesters, diffBuilder); + return diffBuilder.getMessage(); + }); + } + function butString() { - return actual.calls.count() !== 0 - ? 'But the actual calls were:\n' + prettyPrintedCalls.join(',\n') + '.\n\n' - : 'But it was never called.\n\n'; + switch (actual.calls.count()) { + case 0: + return 'But it was never called.\n\n'; + case 1: + return 'But the actual call was:\n' + prettyPrintedCalls.join(',\n') + '.\n' + getDiffs().join('\n') + '\n\n'; + default: + return 'But the actual calls were:\n' + prettyPrintedCalls.join(',\n') + '.\n\n'; + } } return {