我正在为Hubot(充当Slack bot)编写一个简单的测试,以检查我的bot是否发送了响应触发器的回复。我遵循了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,但没有得到答复。
答案 0 :(得分:1)
我认为问题是缩进错误。
正在向@room.user.say
调用传递一个空函数作为一个承诺解决方案,而不是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']
]