Skip to content

Commit 9a7064f

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

File tree

6 files changed

+248
-68
lines changed

6 files changed

+248
-68
lines changed

npm-debug.log

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
0 info it worked if it ends with ok
2+
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
3+
1 verbose cli 'C:\\Users\\arthur.rouet\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
4+
1 verbose cli 'run',
5+
1 verbose cli 'lint:fix' ]
6+
2 info using [email protected]
7+
3 info using [email protected]
8+
4 verbose run-script [ 'prelint:fix', 'lint:fix', 'postlint:fix' ]
9+
5 info lifecycle [email protected]~prelint:fix: [email protected]
10+
6 silly lifecycle [email protected]~prelint:fix: no script for prelint:fix, continuing
11+
7 info lifecycle [email protected]~lint:fix: [email protected]
12+
8 verbose lifecycle [email protected]~lint:fix: unsafe-perm in lifecycle true
13+
9 verbose lifecycle [email protected]~lint:fix: PATH: C:\Users\arthur.rouet\AppData\Roaming\npm\node_modules\npm\bin\node-gyp-bin;C:\Users\arthur.rouet\Desktop\Angie-bot\BOT_Hello_Eng-master\BotTester\node_modules\.bin;C:\Users\arthur.rouet\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\local\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Users\arthur.rouet\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files\Docker\Docker\Resources\bin;C:\ProgramData\Oracle\Java\javapath;C:\Program Files\Java\jdk1.7.0_80;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\Python27;C:\Users\arthur.rouet\AppData\Local\Programs\Python\Python36-32\Scripts;C:\Users\arthur.rouet\AppData\Local\Programs\Python\Python36-32;C:\Program Files (x86)\Symantec\VIP Access Client;C:\Users\arthur.rouet\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\Program Files\Git\cmd;C:\Program Files\TortoiseGit\bin;C:\Users\arthur.rouet\AppData\Local\Programs\Python\Launcher;C:\Python27\script;C:\Python27;C:\Program Files\Git\;C:\Users\arthur.rouet\AppData\Local\Programs\Python\Python36-32\Scripts;C:\Users\arthur.rouet\AppData\Local\Programs\Python\Python36-32;C:\Program Files\Java\jre1.8.0_131\bin;C:\Users\arthur.rouet\AppData\Roaming\npm;C:\Program Files\Apache\Maven\bin;C:\Program Files\Microsoft VS Code\bin;C:\Users\arthur.rouet\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\Users\arthur.rouet\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Git\usr\bin\vendor_perl;C:\Program Files\Git\usr\bin\core_perl
14+
10 verbose lifecycle [email protected]~lint:fix: CWD: C:\Users\arthur.rouet\Desktop\Angie-bot\BOT_Hello_Eng-master\BotTester
15+
11 silly lifecycle [email protected]~lint:fix: Args: [ '/d /s /c', 'tslint \'src/**/*.ts\' --fix' ]
16+
12 silly lifecycle [email protected]~lint:fix: Returned: code: 2 signal: null
17+
13 info lifecycle [email protected]~lint:fix: Failed to exec lint:fix script
18+
14 verbose stack Error: [email protected] lint:fix: `tslint 'src/**/*.ts' --fix`
19+
14 verbose stack Exit status 2
20+
14 verbose stack at EventEmitter.<anonymous> (C:\Users\arthur.rouet\AppData\Roaming\npm\node_modules\npm\lib\utils\lifecycle.js:279:16)
21+
14 verbose stack at emitTwo (events.js:126:13)
22+
14 verbose stack at EventEmitter.emit (events.js:214:7)
23+
14 verbose stack at ChildProcess.<anonymous> (C:\Users\arthur.rouet\AppData\Roaming\npm\node_modules\npm\lib\utils\spawn.js:40:14)
24+
14 verbose stack at emitTwo (events.js:126:13)
25+
14 verbose stack at ChildProcess.emit (events.js:214:7)
26+
14 verbose stack at maybeClose (internal/child_process.js:925:16)
27+
14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
28+
15 verbose pkgid [email protected]
29+
16 verbose cwd C:\Users\arthur.rouet\Desktop\Angie-bot\BOT_Hello_Eng-master\BotTester
30+
17 error Windows_NT 10.0.16299
31+
18 error argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\arthur.rouet\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "run" "lint:fix"
32+
19 error node v8.9.1
33+
20 error npm v4.0.5
34+
21 error code ELIFECYCLE
35+
22 error [email protected] lint:fix: `tslint 'src/**/*.ts' --fix`
36+
22 error Exit status 2
37+
23 error Failed at the [email protected] lint:fix script 'tslint 'src/**/*.ts' --fix'.
38+
23 error Make sure you have the latest version of node.js and npm installed.
39+
23 error If you do, this is most likely a problem with the bot-tester package,
40+
23 error not with npm itself.
41+
23 error Tell the author that this fails on your system:
42+
23 error tslint 'src/**/*.ts' --fix
43+
23 error You can get information on how to open an issue for this project with:
44+
23 error npm bugs bot-tester
45+
23 error Or if that isn't available, you can get their info via:
46+
23 error npm owner ls bot-tester
47+
23 error There is likely additional logging output above.
48+
24 verbose exit [ 1, true ]

src/ExpectedMessage.ts

Lines changed: 9 additions & 5 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
/**
@@ -146,25 +145,30 @@ export class ExpectedMessage {
146145
* Verfy the incoming message with custom test defined by tester
147146
* If the function that tester defined return an error, make the test break
148147
* If the function return anything else, the test is considered as good
149-
* I've tryed to use promise as parameter, but in a promise we change scope, so the assert doesn't work
150148
* @param {IMessage} outgoingMessage outgoing message being compared
151149
*/
152150
private deepMatchCheckWithFunction(outgoingMessage: IMessage): void {
153151
const functionCollection: Function[] = this.expectedResponseCollection as Function[];
154152
let errorString = '';
153+
const exceptedResponsesStrings = [];
155154
let success = false;
156155
functionCollection.forEach((func: Function) => {
157156
const result = func(outgoingMessage);
158157
if (result instanceof Error) {
159158
errorString += `\n -----------------ERROR-----------------\n\n\n'${result.message}' `;
159+
} else if (typeof result === 'string') {
160+
exceptedResponsesStrings.push(result);
160161
} else {
161162
success = true;
162163
}
163164
});
164165
// ErrorString here, can hold multiples error, if the bot send multiples message in one batching
165166
const error = `Bot should have relied response that matches with function but respond '${outgoingMessage}'` +
166167
` that create the following error(s) '${errorString}'`;
167-
this.internalExpectation.expect(success, error).toBeTrue();
168-
168+
if (exceptedResponsesStrings.length > 0) {
169+
this.checkMessageTextForExactStringMatch(outgoingMessage, exceptedResponsesStrings);
170+
} else {
171+
this.internalExpectation.expect(success, error).toBeTrue();
172+
}
169173
}
170174
}

test/ava/BotTester.ava.spec.ts

Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import test from 'ava';
2-
import { Context, TestContext } from 'ava';
3-
import { IAddress, IMessage, Message, Prompts, Session, UniversalBot } from 'botbuilder';
4-
import { BotTester } from './../../src/BotTester';
5-
import { IConfig } from './../../src/config';
6-
import { TestConnector } from './../../src/TestConnector';
7-
import { getAdaptiveCard, getAdaptiveCardAttachment, getAdaptiveCardMessage } from './../adaptiveCardProvider';
2+
import {Context, TestContext} from 'ava';
3+
import {IAddress, IMessage, Message, Prompts, Session, UniversalBot} from 'botbuilder';
4+
import {BotTester} from './../../src/BotTester';
5+
import {IConfig} from './../../src/config';
6+
import {TestConnector} from './../../src/TestConnector';
7+
import {getAdaptiveCard, getAdaptiveCardAttachment, getAdaptiveCardMessage} from './../adaptiveCardProvider';
88

99
const connector = new TestConnector();
1010

@@ -14,6 +14,7 @@ interface IBotTestContext {
1414

1515
//tslint:disable
1616
type AvaTestContext = TestContext & Context<any>;
17+
1718
//tslint:enable
1819

1920
interface IAvaBotTest extends AvaTestContext {
@@ -110,13 +111,13 @@ test('can inspect session state', (t: IAvaBotTest) => {
110111
bot.dialog('/', [(session: Session) => {
111112
new Prompts.text(session, 'What would you like to set data to?');
112113
}, (session: Session, results) => {
113-
session.userData = { data: results.response };
114+
session.userData = {data: results.response};
114115
session.save();
115116
}]);
116117
//tslint:enable
117118

118119
return new BotTester(bot)
119-
.sendMessageToBot('Start this thing!', 'What would you like to set data to?')
120+
.sendMessageToBot('Start this thing!', 'What would you like to set data to?')
120121
.sendMessageToBotAndExpectSaveWithNoResponse('This is data!')
121122
.checkSession((session: Session) => {
122123
t.not(session.userData, null);
@@ -163,16 +164,18 @@ test('address/multiuser can ensure proper address being used four routing. inclu
163164

164165
addressMultiUserSetup(bot);
165166

166-
const defaultAddress = { channelId: 'console',
167-
user: { id: 'customUser1', name: 'A' },
168-
bot: { id: 'customBot1', name: 'Bot1' },
169-
conversation: { id: 'customUser1Conversation' }
167+
const defaultAddress = {
168+
channelId: 'console',
169+
user: {id: 'customUser1', name: 'A'},
170+
bot: {id: 'customBot1', name: 'Bot1'},
171+
conversation: {id: 'customUser1Conversation'}
170172
};
171173

172-
const user2Address = { channelId: 'console',
173-
user: { id: 'user2', name: 'B' },
174-
bot: { id: 'bot', name: 'Bot' },
175-
conversation: { id: 'user2Conversation' }
174+
const user2Address = {
175+
channelId: 'console',
176+
user: {id: 'user2', name: 'B'},
177+
bot: {id: 'bot', name: 'Bot'},
178+
conversation: {id: 'user2Conversation'}
176179
};
177180

178181
const askForUser1Name = new Message()
@@ -206,16 +209,18 @@ test('address/multiuser Can have a default address assigned to it and communicat
206209

207210
addressMultiUserSetup(bot);
208211

209-
const defaultAddress = { channelId: 'console',
210-
user: { id: 'customUser1', name: 'A' },
211-
bot: { id: 'customBot1', name: 'Bot1' },
212-
conversation: { id: 'customUser1Conversation' }
212+
const defaultAddress = {
213+
channelId: 'console',
214+
user: {id: 'customUser1', name: 'A'},
215+
bot: {id: 'customBot1', name: 'Bot1'},
216+
conversation: {id: 'customUser1Conversation'}
213217
};
214218

215-
const user2Address = { channelId: 'console',
216-
user: { id: 'user2', name: 'B' },
217-
bot: { id: 'bot', name: 'Bot' },
218-
conversation: { id: 'user2Conversation' }
219+
const user2Address = {
220+
channelId: 'console',
221+
user: {id: 'user2', name: 'B'},
222+
bot: {id: 'bot', name: 'Bot'},
223+
conversation: {id: 'user2Conversation'}
219224
};
220225

221226
const askForUser1Name = new Message()
@@ -239,8 +244,8 @@ test('address/multiuser Can have a default address assigned to it and communicat
239244
.toMessage();
240245

241246
// when testing for an address that is not the default for the bot, the address must be passed in
242-
return new BotTester(bot, Object.assign({ defaultAddress }, getTestOptions(t)))
243-
// because user 1 is the default address, the expected responses can be a string
247+
return new BotTester(bot, Object.assign({defaultAddress}, getTestOptions(t)))
248+
// because user 1 is the default address, the expected responses can be a string
244249
.sendMessageToBot(askForUser1Name, 'A')
245250
.sendMessageToBot('What is my name?', user1ExpectedResponse)
246251
.sendMessageToBot(askForUser1Name, user1ExpectedResponse)
@@ -251,10 +256,11 @@ test('address/multiuser Can have a default address assigned to it and communicat
251256
test('can handle batch responses', (t: IAvaBotTest) => {
252257
const bot = t.context.bot;
253258

254-
const CUSTOMER_ADDRESS: IAddress = { channelId: 'console',
255-
user: { id: 'userId1', name: 'user1' },
256-
bot: { id: 'bot', name: 'Bot' },
257-
conversation: { id: 'user1Conversation' }
259+
const CUSTOMER_ADDRESS: IAddress = {
260+
channelId: 'console',
261+
user: {id: 'userId1', name: 'user1'},
262+
bot: {id: 'bot', name: 'Bot'},
263+
conversation: {id: 'user1Conversation'}
258264
};
259265

260266
const msg1 = new Message()
@@ -271,7 +277,7 @@ test('can handle batch responses', (t: IAvaBotTest) => {
271277
bot.send([msg1, msg2]);
272278
});
273279

274-
return new BotTester(bot, Object.assign({ defaultAddress: CUSTOMER_ADDRESS }, getTestOptions(t)))
280+
return new BotTester(bot, Object.assign({defaultAddress: CUSTOMER_ADDRESS}, getTestOptions(t)))
275281
.sendMessageToBot('anything', 'hello', 'there')
276282
.runTest();
277283
});
@@ -319,10 +325,10 @@ test('can do arbitrary work between test steps', (t: IAvaBotTest) => {
319325
});
320326

321327
return new BotTester(bot, getTestOptions(t))
322-
.sendMessageToBot('you say', 'goodbye')
323-
.then(() => responseString = 'hello')
324-
.sendMessageToBot('and i say', 'hello')
325-
.runTest();
328+
.sendMessageToBot('you say', 'goodbye')
329+
.then(() => responseString = 'hello')
330+
.sendMessageToBot('and i say', 'hello')
331+
.runTest();
326332
});
327333

328334
test('can wait between test steps', (t: IAvaBotTest) => {
@@ -374,7 +380,7 @@ test('can apply one or more message filters in the BotTester options for message
374380
const ignoreHowMessage = (message: IMessage) => !message.text.includes('how');
375381
const ignoreAreMessage = (message: IMessage) => !message.text.includes('are');
376382

377-
return new BotTester(bot, Object.assign({ messageFilters: [ignoreHowMessage, ignoreAreMessage]}, getTestOptions(t)))
383+
return new BotTester(bot, Object.assign({messageFilters: [ignoreHowMessage, ignoreAreMessage]}, getTestOptions(t)))
378384
.sendMessageToBot('intro', 'hello', 'you?')
379385
.runTest();
380386
});
@@ -400,10 +406,10 @@ test('change timeout time', (t: IAvaBotTest) => {
400406

401407
const timeout = 750;
402408
bot.dialog('/', (session: Session) => {
403-
setTimeout(() => session.send('hi there'), timeout * 2 );
409+
setTimeout(() => session.send('hi there'), timeout * 2);
404410
});
405411

406-
return t.throws(new BotTester(bot, getTestOptions(t))
412+
return t.throws(new BotTester(bot, getTestOptions(t))
407413
.setTimeout(timeout)
408414
.sendMessageToBot('hey', 'hi there')
409415
.runTest());
@@ -461,3 +467,27 @@ test('can ensure adaptive cards are present, regardless of order', (t: IAvaBotTe
461467
.sendMessageToBot('anything', message2)
462468
.runTest();
463469
});
470+
471+
test('can correctly process function as expected message', (t: IAvaBotTest) => {
472+
const bot = t.context.bot;
473+
474+
bot.dialog('/', (session: Session) => {
475+
session.send('hello!');
476+
session.send('12');
477+
});
478+
479+
return new BotTester(bot, getTestOptions(t))
480+
.sendMessageToBot('Hi', (message: IMessage) => {
481+
if (message.text === 'Hello') {
482+
return true;
483+
} else {
484+
return new Error(`Message : '${message.text}' is not equal to 'Hello'`);
485+
}
486+
}, (message: IMessage) => {
487+
if (parseInt(message.text, 0) % 2 === 0) {
488+
return true;
489+
} else {
490+
return new Error(`Message is not an even number : '${message.text}'`);
491+
}
492+
}).runTest();
493+
});

test/ava/BotTesterFailure.ava.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,27 @@ test('will fail when there are no attachments when they are expected', (t: IAvaB
340340
.sendMessageToBot('anything', expectedMessage)
341341
.runTest());
342342
});
343+
344+
test('Will fail when excepted function throw an error', (t: IAvaBotTest) => {
345+
const bot = t.context.bot;
346+
347+
bot.dialog('/', (session: Session) => {
348+
session.send('hello!');
349+
session.send('13');
350+
});
351+
352+
return t.throws(new BotTester(bot, getTestOptions(t))
353+
.sendMessageToBot('Hi', (message: IMessage) => {
354+
if (message.text === 'Hello') {
355+
return true;
356+
} else {
357+
return new Error(`Message : '${message.text}' is not equal to 'Hello'`);
358+
}
359+
}, (message: IMessage) => {
360+
if (parseInt(message.text, 0) % 2 === 0) {
361+
return true;
362+
} else {
363+
return new Error(`Message is not an even number : '${message.text}'`);
364+
}
365+
}).runTest());
366+
});

0 commit comments

Comments
 (0)