debuggingtestingcoffeescripthubot

AssertionError while testing Hubot script with hubot-test-helper and chai


I'm writing a simple test for my Hubot (which acts as a Slack bot) to check that my bot sends a reply in response to triggers. I've followed the example shown in the docs, but test results in an AssertionError (details below) and I'm not sure why. Any advice would be greatly appreciated.

I assume the issue has to do with the test, not the script (break-start.coffee), since I got the correct reply when I tested the script by sending an actual message to the bot from Slack.

# break-start.coffee
# Basically, the bot says "Later alligator" to any user going on lunch break.

module.exports = (robot) ->
  robot.respond /off to lunch/i, (res) ->
    res.reply('Later alligator')
# break-start-test.coffee

'use strict'

Helper = require('hubot-test-helper')
helper = new Helper('../scripts/break-start.coffee')
request = require('request')
expect = require('chai').expect

describe 'bot responds to user message', ->
  beforeEach ->
    # Set up the room before running the test.
    @room = helper.createRoom()

  afterEach ->
    # Tear it down after the test to free up the listener.
    @room.destroy()

  it 'responds to users who are off to lunch', ->
    @room.user.say('bob', '@hubot Off to lunch').then =>
    expect(@room.messages).to.eql [
        ['bob', '@hubot Off to lunch']
        ['hubot', '@bob Later alligator']
      ]

# The error message

AssertionError: expected [ [ 'bob', '@hubot Off to lunch' ] ] to deeply equal [ Array(2) ]
      + expected - actual

         [
           "bob"
           "@hubot Off to lunch"
         ]
      +  [
      +    "hubot"
      +    "@bob Later alligator"
      +  ]
       ]

By the way, there was an extremely similar question posted here before but it went unanswered.


Solution

  • I think the problem is an indentation error.

    The @room.user.say call is being passed an empty function as a promise resolution rather than the expect block, as this should be indented another level.

    That fits with the result that only one message is in the room, as the expect call got executed before the async @room.user.say() got executed:

    it 'responds to users who are off to lunch', ->
      @room.user.say('bob', '@hubot Off to lunch').then =>
        expect(@room.messages).to.eql [
          ['bob', '@hubot Off to lunch']
          ['hubot', '@bob Later alligator']
        ]