Search code examples
node.jsunit-testingsinon

Why is sinon ignoring my call to another function?


I am using a sinon spy in my unit test. The function I am testing calls the spied function once yet sinon insists it's not calling it at all.

The code being tested:

const blank = ' '

const displayBoard = (board) => {
    console.log('+-+-+-+-+-+-+-+-+-+');
    console.log('| |0|1|2|3|4|5|6|7|')
    console.log('+-+-+-+-+-+-+-+-+-+');
    for (let row = board.length - 1; row > -1; row--) {
        let r = `|${row}|`
        for (let column = 0; column < 8; column++) {
            r += board[row][column][1] + '|';
        }
        console.log(r);
        console.log('+-+-+-+-+-+-+-+-+-+');
    }
};

const initialise = () => {
    let board = [];
    board[0] = [['b', 'w'], ['w', blank], ['b', 'w'], ['w', blank], ['b', 'w'], ['w', blank], ['b', 'w'], ['w', blank]];
    board[1] = [['w', blank], ['b', 'w'], ['w', blank], ['b', 'w'], ['w', blank], ['b', 'w'], ['w', blank], ['b', 'w']];
    board[2] = [['b', 'w'], ['w', blank], ['b', 'w'], ['w', blank], ['b', 'w'], ['w', blank], ['b', 'w'], ['w', blank]];
    board[3] = [['w', blank], ['b', blank], ['w', blank], ['b', blank], ['w', blank], ['b', blank], ['w', blank], ['b', blank]];
    board[4] = [['b', blank], ['w', blank], ['b', blank], ['w', blank], ['b', blank], ['w', blank], ['b', blank], ['w', blank]];
    board[5] = [['w', blank], ['b', 'b'], ['w', blank], ['b', 'b'], ['w', blank], ['b', 'b'], ['w', blank], ['b', 'b']];
    board[6] = [['b', 'b'], ['w', blank], ['b', 'b'], ['w', blank], ['b', 'b'], ['w', blank], ['b', 'b'], ['w', blank],];
    board[7] = [['w', blank], ['b', 'b'], ['w', blank], ['b', 'b'], ['w', blank], ['b', 'b'], ['w', blank], ['b', 'b']];
    displayBoard(board);
    return board;
};

module.exports = { initialise, displayBoard };

And my unit test:

const draughts = require('../src/draughts');
const mocha = require('mocha');
const chai = require('chai');
const sinon = require('sinon');

const expect = chai.expect;

describe('draughts', ()=>{
    it('should initialise board', ()=>{
        result = draughts.initialise();
        expect(result).to.be.an('array');
    })
    it('should spy the display method', ()=>{
        let spy = sinon.spy(draughts, 'displayBoard');
        draughts.initialise();
        sinon.assert.calledOnce(spy);
    })
});

The message from sinon:

  1. draughts should spy the display method: AssertError: expected displayBoard to be called once but was called 0 times at Object.fail (node_modules\sinon\lib\sinon\assert.js:110:25) at failAssertion (node_modules\sinon\lib\sinon\assert.js:67:20) at Object.assert. [as calledOnce] (node_modules\sinon\lib\sinon\assert.js:93:17) at Context. (test\draughts.test.js:16:22) at processImmediate (internal/timers.js:461:21)

Solution

  • Change your draughts implementation to something like this.

    const draughts  = { initialise, displayBoard };
    module.exports = draughts;
    

    And then inside initialise function you can call displayBoard using draughts.displayBoard(board);

    This way sinon spy can work.