Tuesday, 14 March 2017

Sinon verify method call on a class attribute

With sinon js, how can I verify that this.socket.emit was called inside the save method of ChannelController ?

ChannelController.js :

export default class ChannelController {
  constructor (socket) {
    this.socket = socket
  }

  save (channel) {
    this.socket.emit('channel.created', channel)
  }
}

ChannelControllerTest.js :

import assert from 'assert'
import sinon from 'sinon'
import ChannelController from './ChannelController'
let channelController, socket

describe('ChannelController', () => {
  before(() => {
    socket = { emit: (event, payload) => {} }
    channelController = new ChannelController(socket)
  })

  it('creates a new channel', (done) => {
    let aChannel = { topic: 'awesome topic' }
    channelController.save(aChannel)
  })
})



via AlexB

No comments:

Post a Comment