Skip to content

Commit

Permalink
Test custom error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
santiagodoldan committed Jan 19, 2018
1 parent 49b8732 commit 932bdad
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/ExpectedMessage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IMessage, IEvent } from 'botbuilder';
import { IEvent, IMessage } from 'botbuilder';
import { expect } from './assertionLibraries/IExpectation';

export enum ExpectedMessageType {
Expand Down Expand Up @@ -90,7 +90,7 @@ export class ExpectedMessage {
expectedResponseStrings.length > 1 ? `one of ${expectedResponseStrings}` : expectedResponseStrings[0];

const errorString =
`Bot should have responded with '${errorStringExpectedResponseText}', but was '${outgoingText}`;
`Bot should have responded with '${errorStringExpectedResponseText}', but was '${outgoingText}'`;

expect(expectedResponseStrings, errorString).toInclude(outgoingText);
}
Expand All @@ -105,7 +105,7 @@ export class ExpectedMessage {
const text = outgoingMessage.text;
const regexCollection: RegExp[] = this.expectedResponseCollection as RegExp[];
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 Down
16 changes: 8 additions & 8 deletions src/assertionLibraries/ChaiExpectation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,33 @@ const expectWithDeepMatch: DeepMatch = expect as any;
export class ChaiExpectation implements IExpectation {
private subject: {};
private message: string;

constructor(subject: {}, message?: string) {
this.subject = subject;
this.message = message;
}

public notToBeEmpty(): void {
expect(this.subject, this.message).not.to.be.empty;
}

public toEqual(value: {}): void {
expect(this.subject, this.message).to.equal(value);
}

public toInclude(value: {}): void {
expect(this.subject, this.message).to.include(value);
}

public toBeTrue(): void {
expect(this.subject, this.message).to.be.true;
}

/**
* deeply compares an outgoing message with the expected messages that are considered valid for this test to pass.
* It does a subset comparison, so as long as the expected message is a subset of one of the expected responses, this will pass
*
* @param outgoingMessage actual message that bot sends
*
* @param outgoingMessage actual message that bot sends
*/
public toDeeplyInclude(outgoingMessage: IMessage): void {
const expectedResponseCollectionAsIMessage: IMessage[] = this.subject as IMessage[];
Expand Down
18 changes: 15 additions & 3 deletions test/BotTesterFailure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('BotTester', () => {
expect(new BotTester(bot)
.sendMessageToBot('Hola!', 'NOPE')
.runTest()
).to.eventually.be.rejectedWith('expected [ \'NOPE\' ] to include \'hello!\'').notify(done);
).to.eventually.be.rejectedWith('Bot should have responded with \'NOPE\', but was \'hello!\'').notify(done);
});

it('will fail if one of multiple responses is incorrect', (done: Function) => {
Expand All @@ -38,7 +38,19 @@ describe('BotTester', () => {
expect(new BotTester(bot)
.sendMessageToBot('Hola!', 'hello!', 'NOPE')
.runTest()
).to.eventually.be.rejectedWith('expected [ \'NOPE\' ] to include \'how are you doing?\'').notify(done);
).to.eventually.be.rejectedWith('Bot should have responded with \'NOPE\', but was \'how are you doing?\'').notify(done);
});

it('it will fail if an empty collection is given', () => {
bot.dialog('/', (session: Session) => {
session.send('hello!');
});

try {
new BotTester(bot).sendMessageToBot('Hola!', []);
} catch (error) {
expect(error.message).to.include('expected response collections cannot be empty');
}
});

it('Will fail if response is not in the random response collection', (done: Function) => {
Expand Down Expand Up @@ -177,7 +189,7 @@ describe('BotTester', () => {
expect(new BotTester(bot)
.sendMessageToBot('abcd', numberRegex)
.runTest()
).to.be.rejected.notify(done);
).to.eventually.be.rejectedWith('\'abcd\' did not match any regex in /^\\d+/').notify(done);
});

it('can timeout', (done: Function) => {
Expand Down

0 comments on commit 932bdad

Please sign in to comment.