I am curious about how I can unit test the bot without using an emulator. Either it can be hosting the bot in unit test project with the in-memory server hosting or communicate the bot with Direct Line API. In both ways, the bot should run locally.
I can't host the bot with Microsoft.AspNetCore.Hosting in my test framework(if hosted, don't know what endpoint the bot communicates).
If I use the bot Startup class with WebHostBuilder in console app the bot is up and running at localhost:5000.
In Console App: IWebHostBuilder builder = new WebHostBuilder().UseStartup
Now how do I make call to bot?
This is an advanced topic (for now). You can look at one of our internal tool, called "Emulator Core CLI", https://github.com/Microsoft/BotFramework-Emulator/tree/master/packages/emulator/cli.
Emulator Core is the server that emulate Direct Line service. You can use it without Emulator app (via the CLI). And it's exactly the same copy we bundle in Emulator app.
Today, we don't publish the CLI because it's not polished yet. You can clone Emulator repos and build it manually. We use this tool internally for hosting bot in offline mode, mainly for testing Web Chat today (or building bot while we are on a plane.)
Before using this tool, it will be helpful if you understand how Web Chat, Direct Line service, and Bot communicate with each other:
Note that since the bot is both HTTP server/client. If all 3 components (Web Chat, Emulator Core, Bot) are hosted locally, it should be kinda trivial to setup. But if you are using a local Emulator Core to talk to a production bot on Azure Bot Services (i.e. Azure Web App), you will need to use ngrok to make your Emulator Core outside of your firewall for the bot to call. We call this "customizing service URL", which is customizing the endpoint for the bot to call back (a.k.a. service URL). This feature is in Emulator app ("the ngrok checkbox") but you will need to do it manually if you go thru CLI.
After you setup all 3 components correctly and they can talk to each other. You can start replacing Web Chat by either BotFramework-DirectLineJS or Web Chat Core (BotFramework-WebChat-Core). Then you could write a test harness to use either one of the packages, to talk to your bot.
The difference of DirectLineJS or Web Chat Core is about the level of abstractions:
DirectLineJS was not designed to run outside of a browser. But you can follow notes here to make DirectLineJS works under Node.js (and also Web Chat Core). We have a work item to make DirectLineJS work under Node.js without polyfills.
So after you setup DirectLineJS or Web Chat Core, you will be able to write a test harness to talk to your bot in offline mode.
To be honest, the whole setup is not trivial. Also to be fair, the code that do heavy-lifting is ready to use today. While we are focusing on big things (a.k.a. main story, e.g. Emulator v4, rewrite of Web Chat, new SDK), we are also paving the way to make bot development easier (a.k.a. side stories). When all the components are cooked in the main story, we will be able to complete the side story in very short timeframe. It's like killing two birds with one stone. One big bird, one small bird. :)
Until then, please let us know about your journey into bot testing. We would love to help.
Most helpful comment
This is an advanced topic (for now). You can look at one of our internal tool, called "Emulator Core CLI", https://github.com/Microsoft/BotFramework-Emulator/tree/master/packages/emulator/cli.
Emulator Core is the server that emulate Direct Line service. You can use it without Emulator app (via the CLI). And it's exactly the same copy we bundle in Emulator app.
Today, we don't publish the CLI because it's not polished yet. You can clone Emulator repos and build it manually. We use this tool internally for hosting bot in offline mode, mainly for testing Web Chat today (or building bot while we are on a plane.)
Before using this tool, it will be helpful if you understand how Web Chat, Direct Line service, and Bot communicate with each other:
Note that since the bot is both HTTP server/client. If all 3 components (Web Chat, Emulator Core, Bot) are hosted locally, it should be kinda trivial to setup. But if you are using a local Emulator Core to talk to a production bot on Azure Bot Services (i.e. Azure Web App), you will need to use ngrok to make your Emulator Core outside of your firewall for the bot to call. We call this "customizing service URL", which is customizing the endpoint for the bot to call back (a.k.a. service URL). This feature is in Emulator app ("the ngrok checkbox") but you will need to do it manually if you go thru CLI.
After you setup all 3 components correctly and they can talk to each other. You can start replacing Web Chat by either
BotFramework-DirectLineJSor Web Chat Core (BotFramework-WebChat-Core). Then you could write a test harness to use either one of the packages, to talk to your bot.The difference of DirectLineJS or Web Chat Core is about the level of abstractions:
DirectLineJS was not designed to run outside of a browser. But you can follow notes here to make DirectLineJS works under Node.js (and also Web Chat Core). We have a work item to make DirectLineJS work under Node.js without polyfills.
So after you setup DirectLineJS or Web Chat Core, you will be able to write a test harness to talk to your bot in offline mode.
To be honest, the whole setup is not trivial. Also to be fair, the code that do heavy-lifting is ready to use today. While we are focusing on big things (a.k.a. main story, e.g. Emulator v4, rewrite of Web Chat, new SDK), we are also paving the way to make bot development easier (a.k.a. side stories). When all the components are cooked in the main story, we will be able to complete the side story in very short timeframe. It's like killing two birds with one stone. One big bird, one small bird. :)
Until then, please let us know about your journey into bot testing. We would love to help.