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.
This commit is contained in:
Nito Buendia
2022-03-17 21:06:51 +08:00
parent a8a6577cd7
commit e470fb56d7
2 changed files with 33 additions and 22 deletions

View File

@@ -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()

View File

@@ -1,4 +1,4 @@
getJasmineRequireObj().toHaveSpyInteractions = function(j$) {
getJasmineRequireObj().toHaveSpyInteractions = function (j$) {
var getErrorMsg = j$.formatErrorMsg(
'<toHaveSpyInteractions>',
'expect(<spyObj>).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;