microsoftly/BotTester

Getting false positives

Closed this issue · 4 comments

Hello,

I'm trying to use BotTester to test one our internal libraries and I'm having some issues. All my specs seem to run fine when I add failing cases. It seems that the runner finishes before the test can actually execute. If I add the done function to it it runs correctly and failure is shown.

Anyone having the same problem ?

Can you add repro steps?

Sure. This is my test file.

import "mocha"
import { expect } from "chai"

import { BotTester } from "bot-tester"
import { UniversalBot, ConsoleConnector, IIdentity } from 'botbuilder'
import { CommandLineLibrary } from '../src/library'

describe("CommandLineLibrary", () => {
  let bot: UniversalBot
  let connector: ConsoleConnector
  let library: CommandLineLibrary

  beforeEach(() => {
    library = new CommandLineLibrary('test-cmd', {
      isAuthorized: async (user: IIdentity) => user.name == 'user1'
    })

    connector = new ConsoleConnector()
    bot = new UniversalBot(connector)

    bot.recognizer({
      recognize: (context, done) => {
        done(null, { score: 1.0, intent: 'test-cmd' })
      }
    })

    bot.library(library)
  })

  describe("when a required option is missing", () => {
    beforeEach(() => {
      library.command('subcmd', {
        options: [
          {
            name: 'foo_required',
            required: true
          }
        ],
        handler: (session, context) => {
          session.endDialog(context.options.foo + context.options.foo_required)
        }
      })
    })

    it("responds with a syntax error", () => {
      new BotTester(bot)
        .sendMessageToBot('test-cmd subcmd', '**Syntax error for command \`subcmd\`**\n\n- Missing required option: foo_required')
        .runTest()
    })
  })

  describe("when all required option are present", () => {
    beforeEach(() => {
      library.command('subcmd', {
        options: [
          {
            name: 'foo_required',
            required: true
          }
        ],
        handler: (session, context) => {
          session.send(context.options.foo_required)
        }
      })
    })

    it("responds executes the handler", () => {
      new BotTester(bot)
        .sendMessageToBot('test-cmd subcmd --foo_required bar', 'baa r')
        .runTest()
    })
  })
})

If I add the done function to the last it for example, I can the test failing.

➜  botbuilder-command-line git:(add-stats-command) ✗ yarn test
yarn test v0.27.5
$ node_modules/mocha/bin/_mocha --require node_modules/ts-node/register tests/library.spec.ts


  CommandLineLibrary
    when a required option is missing
      ✓ responds with a syntax error
    when all required option are present
ASDASDASDSADA
      ✓ responds executes the handler

ASDASDASDSADA

  2 passing (28ms)

Done in 1.18s.
➜  botbuilder-command-line git:(add-stats-command) ✗ yarn test
yarn test v0.27.5
$ node_modules/mocha/bin/_mocha --require node_modules/ts-node/register tests/library.spec.ts


  CommandLineLibrary
    when a required option is missing
      ✓ responds with a syntax error
    when all required option are present
ASDASDASDSADA
ASDASDASDSADA
**Syntax error for command `subcmd`**

- Missing required option: foo_required
bar
Unhandled rejection AssertionError: Bot should have responded with 'baa r', but was 'bar: expected [ 'baa r' ] to include 'bar'

.runTest() returns a promise that resolves when the test finishes (or hangs if the bot expected a response but did not get one).

try adding a return ->

import "mocha"
import { expect } from "chai"

import { BotTester } from "bot-tester"
import { UniversalBot, ConsoleConnector, IIdentity } from 'botbuilder'
import { CommandLineLibrary } from '../src/library'

describe("CommandLineLibrary", () => {
  let bot: UniversalBot
  let connector: ConsoleConnector
  let library: CommandLineLibrary

  beforeEach(() => {
    library = new CommandLineLibrary('test-cmd', {
      isAuthorized: async (user: IIdentity) => user.name == 'user1'
    })

    connector = new ConsoleConnector()
    bot = new UniversalBot(connector)

    bot.recognizer({
      recognize: (context, done) => {
        done(null, { score: 1.0, intent: 'test-cmd' })
      }
    })

    bot.library(library)
  })

  describe("when a required option is missing", () => {
    beforeEach(() => {
      library.command('subcmd', {
        options: [
          {
            name: 'foo_required',
            required: true
          }
        ],
        handler: (session, context) => {
          session.endDialog(context.options.foo + context.options.foo_required)
        }
      })
    })

    it("responds with a syntax error", () => {
      return new BotTester(bot)
        .sendMessageToBot('test-cmd subcmd', '**Syntax error for command \`subcmd\`**\n\n- Missing required option: foo_required')
        .runTest()
    })
  })

  describe("when all required option are present", () => {
    beforeEach(() => {
      library.command('subcmd', {
        options: [
          {
            name: 'foo_required',
            required: true
          }
        ],
        handler: (session, context) => {
          session.send(context.options.foo_required)
        }
      })
    })

    it("responds executes the handler", () => {
      return new BotTester(bot)
        .sendMessageToBot('test-cmd subcmd --foo_required bar', 'baa r')
        .runTest()
    })
  })
})

@microsoftly You are totally right. Completely forgot about that!