问题描述
我正在为我的Hubot(充当Slack bot)编写一个简单的测试,以检查我的bot是否发送了响应触发器的回复.我遵循了 docs 中显示的示例,但是进行了测试结果为AssertionError
(下面有详细信息),但我不确定为什么.任何建议将不胜感激.
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.
我认为问题与测试有关,而不是脚本(break-start.coffee
),因为通过从Slack向机器人发送实际消息来测试脚本时,我得到了正确的答复.
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.
推荐答案
我认为问题是缩进错误.
I think the problem is an indentation error.
@room.user.say
调用被传递给一个空函数,作为一个承诺解决方案,而不是Expect块,因为应该将其缩进另一个级别.
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.
这符合以下结果:房间中只有一条消息,因为expect
调用是在异步@room.user.say()
执行之前执行的:
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']
]
这篇关于使用Hubot-Test-Helper和Chai测试Hubot脚本时出现AssertionError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!