Modified error message

This commit is contained in:
Eradev
2025-01-18 17:55:00 -05:00
parent 289afbf0a6
commit 888d3b6250
2 changed files with 48 additions and 22 deletions

View File

@@ -26,22 +26,32 @@ describe('toHaveNoOtherSpyInteractions', function() {
}); });
it('fails when there are spy interactions', function() { it('fails when there are spy interactions', function() {
let matcher = jasmineUnderTest.matchers.toHaveNoOtherSpyInteractions(); const matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
});
let matcher = jasmineUnderTest.matchers.toHaveNoOtherSpyInteractions(
matchersUtil
);
let spyObj = jasmineUnderTest let spyObj = jasmineUnderTest
.getEnv() .getEnv()
.createSpyObj('NewClass', ['spyA', 'spyB']); .createSpyObj('NewClass', ['spyA', 'spyB']);
spyObj.spyA(); spyObj.spyA('x');
let result = matcher.compare(spyObj); let result = matcher.compare(spyObj);
expect(result.pass).toBeFalse(); expect(result.pass).toBeFalse();
expect(result.message).toContain( expect(result.message).toContain(
"Unverified spies' calls have been found in:" 'Expected to have no other spy interactions, but it had the following calls:'
); );
}); });
it('shows the right message is negated', function() { it('shows the right message is negated', function() {
let matcher = jasmineUnderTest.matchers.toHaveNoOtherSpyInteractions(); const matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
});
let matcher = jasmineUnderTest.matchers.toHaveNoOtherSpyInteractions(
matchersUtil
);
let spyObj = jasmineUnderTest let spyObj = jasmineUnderTest
.getEnv() .getEnv()
.createSpyObj('NewClass', ['spyA', 'spyB']); .createSpyObj('NewClass', ['spyA', 'spyB']);
@@ -52,7 +62,7 @@ describe('toHaveNoOtherSpyInteractions', function() {
let result = matcher.compare(spyObj); let result = matcher.compare(spyObj);
expect(result.pass).toBeFalse(), expect(result.pass).toBeFalse(),
expect(result.message).toContain( expect(result.message).toContain(
"Unverified spies' calls have been found in:" 'Expected to have no other spy interactions, but it had the following calls:'
); );
}); });
@@ -67,7 +77,9 @@ describe('toHaveNoOtherSpyInteractions', function() {
let result = matcher.compare(spyObj); let result = matcher.compare(spyObj);
expect(result.pass).toBeTrue(); expect(result.pass).toBeTrue();
expect(result.message).toContain("Spies' calls are all verified."); expect(result.message).toContain(
"Expected to have other spy interactions but it didn't"
);
}); });
it(`throws an error if a non-object is passed`, function() { it(`throws an error if a non-object is passed`, function() {
@@ -117,7 +129,9 @@ describe('toHaveNoOtherSpyInteractions', function() {
const matchersUtil = new jasmineUnderTest.MatchersUtil({ const matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter() pp: jasmineUnderTest.makePrettyPrinter()
}), }),
matcher = jasmineUnderTest.matchers.toHaveNoOtherSpyInteractions(), matcher = jasmineUnderTest.matchers.toHaveNoOtherSpyInteractions(
matchersUtil
),
toHaveBeenCalledWithMatcher = jasmineUnderTest.matchers.toHaveBeenCalledWith( toHaveBeenCalledWithMatcher = jasmineUnderTest.matchers.toHaveBeenCalledWith(
matchersUtil matchersUtil
), ),

View File

@@ -29,7 +29,7 @@ getJasmineRequireObj().toHaveNoOtherSpyInteractions = function(j$) {
result.pass = true; result.pass = true;
let hasSpy = false; let hasSpy = false;
const unexpectedCallsIn = []; const unexpectedCalls = [];
for (const spy of Object.values(actual)) { for (const spy of Object.values(actual)) {
if (!j$.isSpy(spy)) { if (!j$.isSpy(spy)) {
@@ -38,14 +38,20 @@ getJasmineRequireObj().toHaveNoOtherSpyInteractions = function(j$) {
hasSpy = true; hasSpy = true;
if (!spy.calls.all().every(call => call.verified)) { const unverifiedCalls = spy.calls
unexpectedCallsIn.push([ .all()
spy.and.identity, .filter(call => !call.verified);
spy.calls.unverifiedCount()
]);
if (unverifiedCalls.length > 0) {
result.pass = false; result.pass = false;
} }
unverifiedCalls.forEach(unverifiedCall => {
unexpectedCalls.push([
spy.and.identity,
matchersUtil.pp(unverifiedCall.args)
]);
});
} }
if (!hasSpy) { if (!hasSpy) {
@@ -56,15 +62,21 @@ getJasmineRequireObj().toHaveNoOtherSpyInteractions = function(j$) {
); );
} }
result.message = result.pass if (result.pass) {
? "Spies' calls are all verified." result.message =
: "Unverified spies' calls have been found in: " + "Expected to have other spy interactions but it didn't.";
unexpectedCallsIn } else {
.map( const ppUnexpectedCalls = unexpectedCalls
([spyName, unverifiedCount]) => .map(
`${spyName} (${unverifiedCount} unverified call(s))` ([spyName, arguments]) => ` ${spyName} called with ${arguments}`
) )
.join(', '); .join(',\n');
result.message =
'Expected to have no other spy interactions, but it had the following calls:\n' +
ppUnexpectedCalls +
'.\n\n';
}
return result; return result;
} }