title: Sinon spy.called no working
body: <h2>Background</h2>
  
  <p>I have a small server that receives data from a machine. Every time I receive a message I call a function in a dispatcher object that simply <code>console.log</code>s everything it receives.</p>
  
  <h2>Problem</h2>
  
  <p>The code works well as I can see the <code>console.log</code>s in the console, but Sinon <code>spy.called</code> doesn't work. It is always <code>false</code> no matter how many times I call <code>dispatcher.onMessage</code>.</p>
  
  <h2>Code</h2>
  
  <p><strong>server.js</strong></p>
  
  <pre><code>const eventDispatcher = {
      onMessage: console.log,
  };
  
  const server = (dispatcher = eventDispatcher) =&gt; {
  
  
      //this gets called everytime the server receives a message
      const onData = data =&gt; {
  
          //Process data
          //....
          dispatcher.onMessage(data);
      };
  
  
      const getDispatcher = () =&gt; dispatcher;
  
      return Object.freeze({
          getDispatcher
      });
  };
  </code></pre>
  
  <p><strong>test.js</strong></p>
  
  <pre><code>describe("message sender", () =&gt; {
  
      const myServer = serverFactory();
  
  
      it("should send information to server", () =&gt; {
          dummyMachine.socket.write("Hello World!\r\n");
  
          const dataSpy = sinon.spy(myServer.getDispatcher(), "onMessage");
          expect(dataSpy.called).to.be.true; //always fails!
      });
  
  });
  </code></pre>
  
  <h2>Research</h2>
  
  <p>After reading similar posts I believe this happens due to some layer of indirection, as pointed in:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/26041079/sinon-spy-is-not-called-if-the-spied-method-is-called-indirectly">Sinon Spy is not called if the spied method is called indirectly</a></li>
  </ul>
  
  <p>And should be fixed via using <code>this</code>:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/34795934/sinon-spy-on-a-method-is-not-invoked">Sinon.spy on a method is not invoked</a></li>
  </ul>
  
  <p>However, looking at my code I really can't get what I am missing. </p>
  
  <h2>Question</h2>
  
  <ul>
  <li>What am I doing wrong?</li>
  </ul>
  
  <h2>MCVE</h2>
  
  <p><strong>Directory Structure</strong></p>
  
  <pre><code> Project_Folder
   |____package.json
   |____server.js
   |____test
        |____ dummyMachine_spec.js
  </code></pre>
  
  <p><strong>package.json</strong></p>
  
  <pre><code>{
    "name": "sinon-question",
    "version": "1.0.0",
    "description": "MCVE about a dummy machine connecting to a server for StackOverflow",
    "main": "server.js",
    "scripts": {
      "test": "NODE_ENV=test mocha --reporter spec --slow 5000 --timeout 5000 test/*_spec.js || true"
    },
    "author": "Pedro Miguel P. S. Martins",
    "license": "ISC",
    "devDependencies": {
      "chai": "^3.5.0",
      "mocha": "^3.3.0",
      "sinon": "^2.2.0"
    },
    "dependencies": {
      "net": "^1.0.2"
    }
  }
  </code></pre>
  
  <p><strong>server.js</strong></p>
  
  <pre><code>"use strict";
  
  const net = require("net");
  
  const eventDispatcher = {
      onMessage: console.log,
  };
  
  const server = (dispatcher = eventDispatcher) =&gt; {
  
      let serverSocket;
  
      const onData = data =&gt; {
          //Process data
          dispatcher.onMessage(`I am server and I got ${data}`);
      };
  
      const start = (connectOpts) =&gt; {
          return new Promise(fulfil =&gt; {
              serverSocket = net.createConnection(connectOpts, () =&gt; {
                  serverSocket.on("data", onData);   
                  fulfil();
              });
          });
      };
  
      const stop = () =&gt; serverSocket.destroy();
  
      const getDispatcher = () =&gt; dispatcher;
  
      return Object.freeze({
          start,
          stop,
          getDispatcher
      });
  };
  
  module.exports = server;
  </code></pre>
  
  <p><strong>test/dummyMachine.js</strong></p>
  
  <pre><code>"use strict";
  
  
  const chai = require("chai"),
      expect = chai.expect;
  
  const sinon = require("sinon");
  const net = require("net");
  const serverFactory = require("../server.js");
  
  describe("Dummy Machine", () =&gt; {
  
      const dummyMachine = {
          IP: "localhost",
          port: 4002,
          server: undefined,
          socket: undefined
      };
  
      const server = serverFactory();
  
      before("Sets up dummyReader and server", done =&gt; {
  
          dummyMachine.server = net.createServer(undefined, socket =&gt; {
              dummyMachine.socket = socket;
          });
  
          dummyMachine.server.listen(
              dummyMachine.port,
              dummyMachine.IP,
              undefined,
              () =&gt; {
                  server.start({
                      host: "localhost",
                      port: 4002
                  })
                  .then(done);
              }
          );
      });
  
      after("Kills dummyReader and server", () =&gt; {
          server.stop();
          dummyMachine.server.close();
      });
  
      it("should connect to server", done =&gt; {
          dummyMachine.server.getConnections((err, count) =&gt; {
              expect(err).to.be.null;
              expect(count).to.eql(1);
              done();
          });
  
      });
  
      it("should send information to server", () =&gt; {
          dummyMachine.socket.write("Hello World\r\n");
  
          const dataSpy = sinon.spy(server.getDispatcher(), "onMessage");
          expect(dataSpy.called).to.be.true; //WORK DAAMN YOU!
      });
  });
  </code></pre>
  
  <h2>Instructions for MCVE</h2>
  
  <ol>
  <li>Download the files and create the directory structure indicated.</li>
  <li>Enter project folder and type <code>npm install</code> on a terminal </li>
  <li>Type <code>npm test</code></li>
  </ol>
  
  <p>The first test should pass, meaning a connection is in fact being made. </p>
  
  <p>The second test will fail, even though you get the console log, proving that <code>onMessage</code> was called.</p>