使用 hubot-test-helper 和 chai 测试 Hubot 脚本时出现 AssertionError

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

我正在为我的 Hubot(充当 Slack 机器人)编写一个简单的测试,以检查我的机器人是否发送回复以响应触发器。我已按照 docs 中显示的示例进行操作,但测试结果为 AssertionError(详情如下),但我不确定原因。任何建议将不胜感激。

我认为问题与测试有关,而不是脚本 (break-start.coffee),因为我在通过从 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"
      +  ]
       ]

顺便说一句,之前在这里发过一个非常相似的question,但没有得到回复。

我认为问题是缩进错误。

@room.user.say 调用被传递一个空函数作为 promise 解决方案而不是 expect 块,因为这应该缩进另一个级别。

这符合房间里只有一条消息的结果,因为 expect 调用在异步 @room.user.say() 执行之前执行:

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']
    ]