Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ yarn-error.log
.coveralls.yml
.nyc_output
coverage
*.ava.spec.js
*.ava.spec.js
.idea
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,28 @@ describe('BotTester', () => {
.runTest();
});
```

# Make your own tests
```javascript
it("should make you able to write your own tests", () => {
bot.dialog('/', (session) => {
session.send("Hello");
session.send("12");
});
return botTester.sendMessageToBot('Bonjour',(message) => {
if (message.text === "Hello") {
return true;
} else {
return new Error("Message : " + message.text + "is not equal to 'Hello'")
}
},(message)=>{
if (parseInt(message.text,0) % 2 === 0) {
return true;
} else {
return new Error("Message is not an even number : " + message.text);
}
}).runTest();
});
```
# Address/multiuser cases
```javascript
describe('Address/multi user', () => {
Expand Down
51 changes: 41 additions & 10 deletions src/ExpectedMessage.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { IEvent, IMessage } from 'botbuilder';
import { BotTesterExpectation } from './assertionLibraries/BotTesterExpectation';
import { IConfig } from './config';
import {IEvent, IMessage} from 'botbuilder';
import {assert} from 'chai';
import {BotTesterExpectation} from './assertionLibraries/BotTesterExpectation';
import {IConfig} from './config';

export enum ExpectedMessageType {
String,
IMessage,
Regex
Regex,
Function
}

/**
* Types accepted for responses checkers
*/
export type PossibleExpectedMessageType = string | IMessage | RegExp | IEvent;
export type PossibleExpectedMessageType = string | IMessage | RegExp | IEvent | Function;

/**
* Response expectations area always collections. The collection is the set of possible responses, chosen at random. If the collection size
Expand All @@ -26,6 +28,8 @@ function getExpectedMessageType(expectedResponseCollection: PossibleExpectedMess
return ExpectedMessageType.String;
} else if (firstElt.constructor.name === 'RegExp') {
return ExpectedMessageType.Regex;
} else if (firstElt.constructor.name === 'Function') {
return ExpectedMessageType.Function;
} else {
return ExpectedMessageType.IMessage;
}
Expand All @@ -42,10 +46,8 @@ export class ExpectedMessage {
*/
private readonly expectedResponseCollection: PossibleExpectedMessageCollections;

constructor(
config: IConfig,
expectedResponseCollection: PossibleExpectedMessageType | PossibleExpectedMessageCollections
) {
constructor(config: IConfig,
expectedResponseCollection: PossibleExpectedMessageType | PossibleExpectedMessageCollections) {
this.internalExpectation = new BotTesterExpectation(config);

if (!(expectedResponseCollection instanceof Array)) {
Expand Down Expand Up @@ -73,6 +75,9 @@ export class ExpectedMessage {
// doing this check will highlight if the diff in text instead of a large IMessage diff
this.deepMessageMatchCheck(outgoingMessage);
break;
case ExpectedMessageType.Function:
this.deepMatchCheckWithFunction(outgoingMessage);
break;
default:
this.internalExpectation.expect(outgoingMessage.type).toEqual('save');
}
Expand Down Expand Up @@ -111,7 +116,7 @@ export class ExpectedMessage {
const regexCollection: RegExp[] = this.expectedResponseCollection as RegExp[];

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

/**
Expand All @@ -136,4 +141,30 @@ export class ExpectedMessage {

this.internalExpectation.expect(expectedResponseCollectionAsIMessage).toDeeplyInclude(outgoingMessage);
}

/**
* Verfy the incoming message with custom test defined by tester
* If the function that tester defined return an error, make the test break
* If the function return anything else, the test is considered as good
* I've tryed to use promise as parameter, but in a promise we change scope, so the assert doesn't work
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] spelling error tryed -> tried

* @param {IMessage} outgoingMessage outgoing message being compared
*/
private deepMatchCheckWithFunction(outgoingMessage: IMessage): void {
const functionCollection: Function[] = this.expectedResponseCollection as Function[];
let errorString = '';
let success = false;
functionCollection.forEach((func: Function) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried bluebird Promise.map to allow for promise cases?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but we are in the promise context, the "this.internalExpectation.expect(success, error).toBeTrue();" create an error but test still a success

const result = func(outgoingMessage);
if (result instanceof Error) {
errorString += `\n -----------------ERROR-----------------\n\n\n'${result.message}' `;
} else {
success = true;
}
});
// ErrorString here, can hold multiples error, if the bot send multiples message in one batching
const error = `Bot should have relied response that matches with function but respond '${outgoingMessage}'` +
` that create the following error(s) '${errorString}'`;
this.internalExpectation.expect(success, error).toBeTrue();

}
}
2 changes: 1 addition & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
--require ts-node/register
--recursive
test/*.spec.ts
test/**/*mocha.spec.ts