From e470fb56d74817e9e164ae3ba552d4faa9e2efa6 Mon Sep 17 00:00:00 2001 From: Nito Buendia Date: Thu, 17 Mar 2022 21:06:51 +0800 Subject: [PATCH] Refactor error message to account for negate comparisons The message return on negate clause was not expected. This makes it negative to match expectation. This also add tests for the change, and renames some tests to make it more clear. --- .../matchers/toHaveSpyInteractionsSpec.js | 35 ++++++++++++------- src/core/matchers/toHaveSpyInteractions.js | 20 ++++++----- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/spec/core/matchers/toHaveSpyInteractionsSpec.js b/spec/core/matchers/toHaveSpyInteractionsSpec.js index 2a7bb436..68ca3e91 100755 --- a/spec/core/matchers/toHaveSpyInteractionsSpec.js +++ b/spec/core/matchers/toHaveSpyInteractionsSpec.js @@ -1,5 +1,5 @@ describe('toHaveSpyInteractions', function() { - it('detects spy interactions', function() { + it('passes when there are spy interactions', function() { let matcher = jasmineUnderTest.matchers.toHaveSpyInteractions(); let spyObj = jasmineUnderTest .getEnv() @@ -9,12 +9,9 @@ describe('toHaveSpyInteractions', function() { let result = matcher.compare(spyObj); expect(result.pass).toBe(true); - expect(result.message).toContain( - 'Expected spy object spies to have been called' - ); }); - it('detects multiple spy interactions', function() { + it('passes when there are multiple spy interactions', function() { let matcher = jasmineUnderTest.matchers.toHaveSpyInteractions(); let spyObj = jasmineUnderTest .getEnv() @@ -26,12 +23,9 @@ describe('toHaveSpyInteractions', function() { let result = matcher.compare(spyObj); expect(result.pass).toBe(true); - expect(result.message).toContain( - 'Expected spy object spies to have been called' - ); }); - it('detects no spy interactions', function() { + it('fails when there are no spy interactions', function() { let matcher = jasmineUnderTest.matchers.toHaveSpyInteractions(); let spyObj = jasmineUnderTest .getEnv() @@ -44,7 +38,22 @@ describe('toHaveSpyInteractions', function() { ); }); - it('ignores non-observed spy object interactions', function() { + it('shows the right message is negated', function () { + let matcher = jasmineUnderTest.matchers.toHaveSpyInteractions(); + let spyObj = jasmineUnderTest + .getEnv() + .createSpyObj('NewClass', ['spyA', 'spyB']); + + spyObj.spyA(); + + let result = matcher.compare(spyObj); + expect(result.pass).toBe(true); + expect(result.message).toContain( // Will be shown only on negate. + 'Expected spy object spies not to have been called' + ); + }); + + it('fails when only non-observed spy object interactions are interacted', function() { let matcher = jasmineUnderTest.matchers.toHaveSpyInteractions(); let spyObj = jasmineUnderTest .getEnv() @@ -60,7 +69,7 @@ describe('toHaveSpyInteractions', function() { ); }); - it(`throws error if a non-object is passed`, function() { + it(`throws an error if a non-object is passed`, function() { let matcher = jasmineUnderTest.matchers.toHaveSpyInteractions(); expect(function() { @@ -76,7 +85,7 @@ describe('toHaveSpyInteractions', function() { }).toThrowError(Error, /Expected a spy object, but got/); }); - it('throws error if arguments are passed', function() { + it('throws an error if arguments are passed', function() { let matcher = jasmineUnderTest.matchers.toHaveSpyInteractions(); let spyObj = jasmineUnderTest .getEnv() @@ -87,7 +96,7 @@ describe('toHaveSpyInteractions', function() { }).toThrowError(Error, /Does not take arguments/); }); - it('throws error if spy object has no spies', function() { + it('throws an error if the spy object has no spies', function() { let matcher = jasmineUnderTest.matchers.toHaveSpyInteractions(); const spyObj = jasmineUnderTest .getEnv() diff --git a/src/core/matchers/toHaveSpyInteractions.js b/src/core/matchers/toHaveSpyInteractions.js index 58f6d44d..c2c83eb2 100755 --- a/src/core/matchers/toHaveSpyInteractions.js +++ b/src/core/matchers/toHaveSpyInteractions.js @@ -1,4 +1,4 @@ -getJasmineRequireObj().toHaveSpyInteractions = function(j$) { +getJasmineRequireObj().toHaveSpyInteractions = function (j$) { var getErrorMsg = j$.formatErrorMsg( '', 'expect().toHaveSpyInteractions()' @@ -14,7 +14,7 @@ getJasmineRequireObj().toHaveSpyInteractions = function(j$) { */ function toHaveSpyInteractions(matchersUtil) { return { - compare: function(actual) { + compare: function (actual) { var result = {}; if (!j$.isObject_(actual)) { @@ -48,16 +48,18 @@ getJasmineRequireObj().toHaveSpyInteractions = function(j$) { ); } - let resultMessage = 'Expected spy object spies to have been called'; + let resultMessage; if (result.pass) { - resultMessage += ', the following spies were called: '; - resultMessage += calledSpies - .map(([spyName, spyCount]) => { + resultMessage = + 'Expected spy object spies not to have been called, ' + + 'but the following spies were called: '; + resultMessage += calledSpies.map(([spyName, spyCount]) => { return `${spyName} called ${spyCount} time(s)`; - }) - .join(', '); + }).join(', '); } else { - resultMessage += ', but no spies were called.'; + resultMessage = + 'Expected spy object spies to have been called, ' + + 'but no spies were called.'; } result.message = resultMessage;