[英]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. 我正在为Hubot(充当Slack机器人)编写一个简单的测试,以检查我的机器人是否发送了响应触发器的回复。 I've followed the example shown in the docs , but test results in an
AssertionError
(details below) and I'm not sure why. 我遵循了docs中显示的示例,但是测试导致出现
AssertionError
(下面的详细信息),但我不确定为什么。 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
),因为通过从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. 我认为问题是缩进错误。
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. 正在向
@room.user.say
调用传递一个空函数作为承诺解决方案,而不是@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: 这与只有一条消息在房间中的结果
@room.user.say()
,因为在执行异步@room.user.say()
之前执行了expect
调用:
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']
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.