0

i'm trying to stub a socketcluster-client to emit events to the socketserver.

i keep getting the below error from sinon

 TypeError: socket.emit is not a function

this is my test-suite

import {expect} from 'chai';
import sinon from 'sinon'
import io from 'socketcluster-client';
import testServer from '../../server/server.js';

describe('httpServer',() => {


  beforeEach(() => {
    testServer(4000)
  })

  it('should respond to the ping event',() => {
    var socket =sinon.stub(io,'connect')

    var message = 'house'
    socket.emit('ping',message);
  })

})

the connect function usually needs to be called with an argument specifying the port io.connect({port:4000})

how do i stub this with sinon?

i would ideally like to emit events from the stub to check my server response

Jayaram
  • 6,276
  • 12
  • 42
  • 78

1 Answers1

1

You want to use sinon.spy(), not sinon.stub(). The former will call the original function, the latter won't.

Quote from the documentation:

When wrapping an existing function with a stub, the original function is not called.

You also need to make sure that you actually call it, too, which your current code doesn't seem to be doing.

EDIT: from your comments, it seems to me that all you want to do is run a client, send some messages to the server, and check if the server responds properly. You don't need spies/stubs for that.

I don't know socketcluster, but to give an idea on how it can be implemented, here's an example using a simple HTTP server:

describe('httpServer', () => {

  before((done) => {
    http.createServer((req, res) => {
      res.end(req.url === '/ping' ? 'PONG' : 'HELLO WORLD');
    }).listen(4000, done);
  });

  it('should respond to the ping event', (done) => {

    http.get('http://localhost:4000/ping', (res) => {
      let buffers = [];

      res.on('data', (d) => buffers.push(d))
         .on('end', () => {
           let body = Buffer.concat(buffers);
           expect(body.toString()).to.equal('PONG');
           done();
         });
    });

  });
});

(it's a minimal example, as it doesn't check for errors or clean up the HTTP server after the tests have completed)

robertklep
  • 198,204
  • 35
  • 394
  • 381
  • something like this? http://pastebin.com/iym6Fe2v - i'm not sure if i'm actually going about tdd in the right way. is this the correct way to test a socket server by stubbing/spying a socket-client? – Jayaram May 04 '16 at 14:03
  • Let's start at the beginning: what exactly do you want to test? :-D Spies/stubs are meant to "hook" existing functions/methods to see if they get called with particular arguments, or that callers of those functions/methods respond properly to certain conditions. – robertklep May 04 '16 at 14:05
  • i want to emit a 'ping' event from the client and check if the server correctly responds to the 'ping' event (return 'pong', or err depending on payload). the reason why i wanted to use stub/spy was to only test the server response and not the client. – Jayaram May 04 '16 at 14:09
  • @Kunkka I added some additional sample code that may be useful. – robertklep May 04 '16 at 14:26
  • if i dont use sinon/stub to mock out the client? wouldnt this be considered integration testing?if so, do i need to do unit tests? i tried doing this - https://gist.github.com/Kannaj/8a49fd36f9c9877128eaec9d5330f39e – Jayaram May 04 '16 at 15:30
  • @Kunkka it sounded to me like you _wanted_ to do this sort of testing (which could be considered integration testing, yes), with both a server and a client instance. You can unit-test parts of the server (for instance the handler that should respond to "ping" messages), but you'd have to mock parts of the server for that, which totally depends on how the server is implemented. – robertklep May 04 '16 at 17:40