我正在使用Jest为我的机器人编写测试,我有以下代码:
let bot;
let conn;
let index = 0;
let responses = [];
let expectedResponses = [];
beforeEach(() => {
let connector = new builder.ConsoleConnector();
bot = new builder.UniversalBot(connector);
conn = connector;
});
test('test case 1 jeff', () => {
bot.library(require('../dialogs/inputRecognizer').createLibrary());
// the main dialog "/" should start "inputRecognizer:/"
// as it can be seen [https://github.com/Microsoft/BotBuilder/blob/858aad96cdd1fabbf7f9a29ee797eb03111d7cba/Node/core/tests/Dialogs.js][1] the code below seems to be identical to the one shown in examples
bot.dialog('/', [
(session) => {
session.beginDialog('inputRecognizer:/');
}
]);
basicMessages.forEach(o => {if (o.in) {expectedResponses.push(o.in)}});
bot.on('send', function (message) {
responses.push(message.text);
index++;
if (index < testMessages.length) {
conn.processMessage(basicMessages[index].out);
} else {
expect(responses).toEqual(expectedResponses);
}
});
conn.processMessage(basicMessages[0].out);
});
代码运行正常并将我的所有消息输入到连接器中。但是,第二条消息被视为新对话框,以及所有以下消息。所以我得到的是一遍又一遍的对话。
我已经在GitHub上查看了BotFramework测试,看起来他们正在使用类似的方法(触发&#39; send&#39;事件处理程序之外的第一个processMessage,并调用下一个处理程序中的那些)。
如何让processMessage每次都不重启我的对话框?我认为这对我的对话有问题吗?
我的对话框代码(&#34; inputRecognizer:/&#34;):
lib.dialog('/', [
function (session) {
// it always executes this part
builder.Prompts.text(session,
`Please type your inquiry!`);
},
(session, result) => {
if (result.response) {
// and it never gets to here
编辑:basicMessages
就是这样:
module.exports = [
{
out: 'hi',
in: 'Please type your inquiry!',
},
{
out: 'aaa',
in: 'Please type your inquiry!'
},
{
out: 'aaa',
in: 'Please type your inquiry!'
},
{
out: 'aaaa',
in: 'Please type your inquiry!'
},
{
out: 'aa',
in: 'Please type your inquiry!'
}
];
答案 0 :(得分:2)
您的basicMessages
似乎错了:您的expectedResponses
数组看起来像是:
["Please type your inquiry!", "Please type your inquiry!", "Please type your inquiry!", "Please type your inquiry!", "Please type your inquiry!"]
当您将messages.Text
添加到稍后用于断言的responses
列表中时,我不会认为这是预期的。
另外,您不需要createLibrary();
您可以使用bot.library(require('../dialogs/inputRecognizer'));
我重新创建了你的项目(GitHub),并且在提示完成后代码到达了瀑布。