【问题标题】:Unit testing several messages in Bot Framework using Chai使用 Chai 在 Bot Framework 中对多个消息进行单元测试
【发布时间】:2018-06-23 22:53:32
【问题描述】:

我正在使用 Chai 通过单元测试覆盖我的聊天机器人(使用 Bot Framework 构建)。在一种情况下,机器人通过 2 条消息响应用户。我想检查两条消息是否正确。我发现这个测试唯一没有失败的是:

bot.on('send', (message) => {
        expect(message.text).to.satisfy(function(text) {
            if (text === message1 || text === message2 ) {
                return true;
            }
            else {
                return false;
            }
        });


    });

done();

正如我通过测试看到的,回调发生了两次,当机器人正常运行时测试通过;但是,测试并没有真正检查是否以正确的顺序返回了 2 条不同的消息。

有没有办法使用 Chai 的expect 或其他工具来做到这一点?

【问题讨论】:

    标签: node.js unit-testing botframework chai


    【解决方案1】:

    我不确定是否有解决此问题的工具,因为这似乎是一个非常具体的问题。你可以做的是:

    let messages = [message1, message2]
    let index = 0
    bot.on('send', (message) => {
        expect(message.text).toBe(messages[index++]);
    });
    
    done();
    

    我不确定done() 部分。你可能想在index === messages.length时调用它

    【讨论】:

      【解决方案2】:

      例如,如果您使用 sinon 创建回调函数的 spy,您可以断言第一次调用回调是使用消息 1,第二次调用是使用消息 2。 spy.firstCall.callWith(message1); spy.secondCall.callWith(message2); 如果您愿意,可以在这里阅读更多内容 - http://sinonjs.org/releases/v6.0.1/spies/

      【讨论】:

        猜你喜欢
        • 2017-07-22
        • 2018-01-02
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 2013-12-25
        • 2021-09-14
        • 1970-01-01
        • 2019-02-14
        相关资源
        最近更新 更多