Skip to content

Commit fb63b7f

Browse files
committed
[ARO] Add unit test + interpretation of string return
1 parent f2c97dc commit fb63b7f

File tree

3 files changed

+108
-29
lines changed

3 files changed

+108
-29
lines changed

src/ExpectedMessage.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {IEvent, IMessage} from 'botbuilder';
2-
import {assert} from 'chai';
32
import {BotTesterExpectation} from './assertionLibraries/BotTesterExpectation';
43
import {IConfig} from './config';
54

@@ -116,7 +115,7 @@ export class ExpectedMessage {
116115
const regexCollection: RegExp[] = this.expectedResponseCollection as RegExp[];
117116

118117
this.internalExpectation.expect(regexCollection.some((regex: RegExp) => regex.test(text)),
119-
`'${text}' did not match any regex in ${regexCollection}`).toBeTrue();
118+
`'${text}' did not match any regex in ${regexCollection}`).toBeTrue();
120119
}
121120

122121
/**
@@ -152,19 +151,25 @@ export class ExpectedMessage {
152151
private deepMatchCheckWithFunction(outgoingMessage: IMessage): void {
153152
const functionCollection: Function[] = this.expectedResponseCollection as Function[];
154153
let errorString = '';
154+
const exceptedResponsesStrings = [];
155155
let success = false;
156156
functionCollection.forEach((func: Function) => {
157157
const result = func(outgoingMessage);
158158
if (result instanceof Error) {
159159
errorString += `\n -----------------ERROR-----------------\n\n\n'${result.message}' `;
160+
} else if (typeof result === 'string') {
161+
exceptedResponsesStrings.push(result);
160162
} else {
161163
success = true;
162164
}
163165
});
164166
// ErrorString here, can hold multiples error, if the bot send multiples message in one batching
165167
const error = `Bot should have relied response that matches with function but respond '${outgoingMessage}'` +
166168
` that create the following error(s) '${errorString}'`;
167-
this.internalExpectation.expect(success, error).toBeTrue();
168-
169+
if (exceptedResponsesStrings.length > 0) {
170+
this.checkMessageTextForExactStringMatch(outgoingMessage, exceptedResponsesStrings);
171+
} else {
172+
this.internalExpectation.expect(success, error).toBeTrue();
173+
}
169174
}
170175
}

test/mocha/chai/BotTester.mocha.spec.ts

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//```javascript
2-
import { IAddress, IMessage, Message, Prompts, Session, UniversalBot } from 'botbuilder';
2+
import {IAddress, IMessage, Message, Prompts, Session, UniversalBot} from 'botbuilder';
33
import * as chai from 'chai';
44
import * as chaiAsPromised from 'chai-as-promised';
5-
import { BotTester } from './../../../src/BotTester';
6-
import { TestConnector } from './../../../src/TestConnector';
7-
import { getAdaptiveCard, getAdaptiveCardAttachment, getAdaptiveCardMessage } from './../../adaptiveCardProvider';
5+
import {BotTester} from './../../../src/BotTester';
6+
import {TestConnector} from './../../../src/TestConnector';
7+
import {getAdaptiveCard, getAdaptiveCardAttachment, getAdaptiveCardMessage} from './../../adaptiveCardProvider';
88

99
chai.use(chaiAsPromised);
1010
const expect = chai.expect;
@@ -105,12 +105,12 @@ describe('BotTester', () => {
105105
bot.dialog('/', [(session) => {
106106
new Prompts.text(session, 'What would you like to set data to?');
107107
}, (session, results) => {
108-
session.userData = { data: results.response };
108+
session.userData = {data: results.response};
109109
session.save();
110110
}]);
111111

112112
return new BotTester(bot)
113-
.sendMessageToBot('Start this thing!', 'What would you like to set data to?')
113+
.sendMessageToBot('Start this thing!', 'What would you like to set data to?')
114114
.sendMessageToBotAndExpectSaveWithNoResponse('This is data!')
115115
.checkSession((session) => {
116116
expect(session.userData).not.to.be.null;
@@ -152,16 +152,18 @@ describe('BotTester', () => {
152152
//# Address/multiuser cases
153153
//```javascript
154154
describe('Address/multi user', () => {
155-
const defaultAddress = { channelId: 'console',
156-
user: { id: 'customUser1', name: 'A' },
157-
bot: { id: 'customBot1', name: 'Bot1' },
158-
conversation: { id: 'customUser1Conversation' }
155+
const defaultAddress = {
156+
channelId: 'console',
157+
user: {id: 'customUser1', name: 'A'},
158+
bot: {id: 'customBot1', name: 'Bot1'},
159+
conversation: {id: 'customUser1Conversation'}
159160
};
160161

161-
const user2Address = { channelId: 'console',
162-
user: { id: 'user2', name: 'B' },
163-
bot: { id: 'bot', name: 'Bot' },
164-
conversation: { id: 'user2Conversation' }
162+
const user2Address = {
163+
channelId: 'console',
164+
user: {id: 'user2', name: 'B'},
165+
bot: {id: 'bot', name: 'Bot'},
166+
conversation: {id: 'user2Conversation'}
165167
};
166168

167169
beforeEach(() => {
@@ -201,7 +203,7 @@ describe('BotTester', () => {
201203

202204
//## Can have a default address assigned to the bot
203205
//```javascript
204-
// the bot can have a default address that messages are sent to. If needed, the default address can be ignored by sending an IMessage
206+
// the bot can have a default address that messages are sent to. If needed, the default address can be ignored by sending an IMessage
205207
it('Can have a default address assigned to it and communicate to multiple users', () => {
206208
const askForUser1Name = new Message()
207209
.text('What is my name?')
@@ -224,8 +226,8 @@ describe('BotTester', () => {
224226
.toMessage();
225227

226228
// when testing for an address that is not the default for the bot, the address must be passed in
227-
return new BotTester(bot, { defaultAddress })
228-
// because user 1 is the default address, the expected responses can be a string
229+
return new BotTester(bot, {defaultAddress})
230+
// because user 1 is the default address, the expected responses can be a string
229231
.sendMessageToBot(askForUser1Name, 'A')
230232
.sendMessageToBot('What is my name?', user1ExpectedResponse)
231233
.sendMessageToBot(askForUser1Name, user1ExpectedResponse)
@@ -238,10 +240,11 @@ describe('BotTester', () => {
238240
//# Can test batch responses
239241
//```javascript
240242
it('can handle batch responses', () => {
241-
const CUSTOMER_ADDRESS: IAddress = { channelId: 'console',
242-
user: { id: 'userId1', name: 'user1' },
243-
bot: { id: 'bot', name: 'Bot' },
244-
conversation: { id: 'user1Conversation' }
243+
const CUSTOMER_ADDRESS: IAddress = {
244+
channelId: 'console',
245+
user: {id: 'userId1', name: 'user1'},
246+
bot: {id: 'bot', name: 'Bot'},
247+
conversation: {id: 'user1Conversation'}
245248
};
246249

247250
const msg1 = new Message()
@@ -258,7 +261,7 @@ describe('BotTester', () => {
258261
bot.send([msg1, msg2]);
259262
});
260263

261-
return new BotTester(bot, { defaultAddress: CUSTOMER_ADDRESS })
264+
return new BotTester(bot, {defaultAddress: CUSTOMER_ADDRESS})
262265
.sendMessageToBot('anything', 'hello', 'there')
263266
.runTest();
264267
});
@@ -282,6 +285,44 @@ describe('BotTester', () => {
282285
});
283286
//```
284287

288+
//# Can test using Function
289+
//```javascript
290+
it('accepts Function', () => {
291+
bot.dialog('/', (session: Session) => {
292+
session.send('hello!');
293+
session.send('12');
294+
});
295+
296+
const botTester = new BotTester(bot)
297+
.sendMessageToBot('Hi', (message: IMessage) => {
298+
if (message.text === 'hello!') {
299+
return true;
300+
}
301+
}, (message: IMessage) => {
302+
if (parseInt(message.text, 0) % 2 === 0) {
303+
return true;
304+
}
305+
});
306+
307+
return botTester.runTest();
308+
});
309+
//```
310+
311+
//```javascript
312+
it('accepts Function that return string', () => {
313+
bot.dialog('/', (session: Session) => {
314+
session.send('hello!');
315+
});
316+
317+
const botTester = new BotTester(bot)
318+
.sendMessageToBot('Hi', (message: IMessage) => {
319+
return message.text;
320+
});
321+
322+
return botTester.runTest();
323+
});
324+
//```
325+
285326
//# variable # args can have mixed type
286327
//```javascript
287328
it('rest params can have mixed type', () => {
@@ -367,7 +408,7 @@ describe('BotTester', () => {
367408
const ignoreHowMessage = (message) => !message.text.includes('how');
368409
const ignoreAreMessage = (message) => !message.text.includes('are');
369410

370-
return new BotTester(bot, { messageFilters: [ignoreHowMessage, ignoreAreMessage]})
411+
return new BotTester(bot, {messageFilters: [ignoreHowMessage, ignoreAreMessage]})
371412
.sendMessageToBot('intro', 'hello', 'you?')
372413
.runTest();
373414
});
@@ -393,7 +434,7 @@ describe('BotTester', () => {
393434
it('change timeout time', (done) => {
394435
const timeout = 750;
395436
bot.dialog('/', (session) => {
396-
setTimeout(() => session.send('hi there'), timeout * 2 );
437+
setTimeout(() => session.send('hi there'), timeout * 2);
397438
});
398439

399440
expect(new BotTester(bot)

test/mocha/chai/BotTesterFailure.mocha.spec.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('BotTester', () => {
4242
});
4343

4444
// ignore this for now. It's more of a debate as to whether or not the user should know not to do this
45-
xit('it will fail if an empty collection is given', () => {
45+
it('it will fail if an empty collection is given', () => {
4646
bot.dialog('/', (session: Session) => {
4747
session.send('hello!');
4848
});
@@ -193,6 +193,39 @@ describe('BotTester', () => {
193193
).to.eventually.be.rejectedWith('\'abcd\' did not match any regex in /^\\d+/').notify(done);
194194
});
195195

196+
it('will fail if function return an error', (done: Function) => {
197+
bot.dialog('/', (session: Session) => {
198+
session.send('hello!');
199+
session.send('13');
200+
});
201+
202+
const botTester = new BotTester(bot)
203+
.sendMessageToBot('Hi', (message: IMessage) => {
204+
if (message.text === 'hello!') {
205+
return true;
206+
}
207+
}, (message: IMessage) => {
208+
if (parseInt(message.text, 0) % 2 !== 0) {
209+
return new Error(`Message is not an even number : '${message.text}'`);
210+
}
211+
});
212+
213+
expect(botTester.runTest()).to.be.rejected.notify(done);
214+
});
215+
216+
it('will fail if function return a bad string', (done: Function) => {
217+
bot.dialog('/', (session: Session) => {
218+
session.send('hello!');
219+
});
220+
221+
const botTester = new BotTester(bot)
222+
.sendMessageToBot('Hi', (message: IMessage) => {
223+
return 'foo';
224+
});
225+
226+
expect(botTester.runTest()).to.be.rejected.notify(done);
227+
});
228+
196229
it('can timeout', (done: Function) => {
197230
const timeout = 1000;
198231
bot.dialog('/', (session: Session) => {

0 commit comments

Comments
 (0)