AssertionError while testing Hubot script with hub

2019-07-08 10:29发布

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.

1条回答
Luminary・发光体
2楼-- · 2019-07-08 10:56

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']
    ]
查看更多
登录 后发表回答