public async Task TestOperation()
        {
            // Instantiate dialog to test
            IDialog <object> rootDialog = new RootDialog();

            // Create in-memory bot environment
            Func <IDialog <object> > MakeRoot = () => rootDialog;

            using (new FiberTestBase.ResolveMoqAssembly(rootDialog))
                using (var container = Build(Options.MockConnectorFactory | Options.ScopedQueue, rootDialog))
                {
                    // Create a message to send to bot
                    var toBot = DialogTestBase.MakeTestMessage();
                    toBot.From.Id = Guid.NewGuid().ToString();

                    // Act
                    toBot.Text = "operation";
                    IMessageActivity toUser = await GetResponse(container, MakeRoot, toBot);

                    // Assert
                    Assert.AreEqual("請輸入第一個數字: ", toUser.Text);

                    // Act
                    toBot.Text = "123";
                    toUser     = await GetResponse(container, MakeRoot, toBot);

                    // Assert
                    HeroCard heroCard = (HeroCard)toUser.Attachments[0].Content;
                    Assert.AreEqual("請輸入運算子: ", heroCard.Text);
                    Assert.AreEqual("Add", heroCard.Buttons[0].Value);
                    Assert.AreEqual("Sub", heroCard.Buttons[1].Value);
                    Assert.AreEqual("Mul", heroCard.Buttons[2].Value);
                    Assert.AreEqual("Div", heroCard.Buttons[3].Value);

                    // Act
                    toBot.Text = "Add";
                    toUser     = await GetResponse(container, MakeRoot, toBot);

                    // Assert
                    Assert.AreEqual("請輸入第二個數字: ", toUser.Text);

                    // Act
                    toBot.Text = "456";
                    toUser     = await GetResponse(container, MakeRoot, toBot);

                    // Assert
                    Assert.AreEqual("123 + 456 = 579", toUser.Text);
                }
        }
        public async Task ShouldReturnEcho()
        {
            // Instantiate dialog to test
            IDialog <object> rootDialog = new RootDialog();

            // Create in-memory bot environment
            Func <IDialog <object> > MakeRoot = () => rootDialog;

            using (new FiberTestBase.ResolveMoqAssembly(rootDialog))
                using (var container = Build(Options.MockConnectorFactory | Options.ScopedQueue, rootDialog))
                {
                    // Create a message to send to bot
                    var toBot = DialogTestBase.MakeTestMessage();
                    toBot.From.Id = Guid.NewGuid().ToString();
                    toBot.Text    = "hi!";

                    // Send message and check the answer.
                    IMessageActivity toUser = await GetResponse(container, MakeRoot, toBot);

                    // Verify the result
                    Assert.AreEqual(" 你說了 hi!,總共有 3 個字元", toUser.Text);
                }
        }
        public async Task TestHelp()
        {
            // Instantiate dialog to test
            IDialog <object> rootDialog = new RootDialog();

            // Create in-memory bot environment
            Func <IDialog <object> > MakeRoot = () => rootDialog;

            using (new FiberTestBase.ResolveMoqAssembly(rootDialog))
                using (var container = Build(Options.MockConnectorFactory | Options.ScopedQueue, rootDialog))
                {
                    // Create a message to send to bot
                    var toBot = DialogTestBase.MakeTestMessage();
                    toBot.From.Id = Guid.NewGuid().ToString();

                    // Act
                    toBot.Text = "help";
                    IMessageActivity toUser = await GetResponse(container, MakeRoot, toBot);

                    // Assert
                    HeroCard heroCard = (HeroCard)toUser.Attachments[0].Content;
                    Assert.AreEqual("功能清單", heroCard.Title);
                    Assert.AreEqual("Hello", heroCard.Buttons[0].Value);
                    Assert.AreEqual("Operation", heroCard.Buttons[1].Value);
                    Assert.AreEqual("OperationV2", heroCard.Buttons[2].Value);
                    Assert.AreEqual("Cards", heroCard.Buttons[3].Value);
                    Assert.AreEqual("Post", heroCard.Buttons[4].Value);

                    // Act
                    toBot.Text = "Hello";
                    toUser     = await GetResponse(container, MakeRoot, toBot);

                    // Assert
                    Assert.AreEqual("請輸入你的名字", toUser.Text);
                }
        }
Example #4
0
        private async Task EchoDialogFlow(IDialog <object> echoDialog)
        {
            // arrange
            var toBot = DialogTestBase.MakeTestMessage();

            toBot.From.Id = Guid.NewGuid().ToString();
            toBot.Text    = "Test";

            Func <IDialog <object> > MakeRoot = () => echoDialog;

            using (new FiberTestBase.ResolveMoqAssembly(echoDialog))
                using (var container = Build(Options.MockConnectorFactory | Options.ScopedQueue, echoDialog))
                {
                    // act: sending the message
                    IMessageActivity toUser = await GetResponse(container, MakeRoot, toBot);

                    // assert: check if the dialog returned the right response
                    Assert.IsTrue(toUser.Text.StartsWith("1"));
                    Assert.IsTrue(toUser.Text.Contains("Test"));

                    // act: send the message 10 times
                    for (int i = 0; i < 10; i++)
                    {
                        // pretend we're the intercom switch, and copy the bot data from message to message
                        toBot.Text = toUser.Text;
                        toUser     = await GetResponse(container, MakeRoot, toBot);
                    }

                    // assert: check the counter at the end
                    Assert.IsTrue(toUser.Text.StartsWith("11"));

                    toBot.Text = "reset";
                    toUser     = await GetResponse(container, MakeRoot, toBot);

                    // checking if there is any cards in the attachment and promote the card.text to message.text
                    if (toUser.Attachments != null && toUser.Attachments.Count > 0)
                    {
                        var card = (HeroCard)toUser.Attachments.First().Content;
                        toUser.Text = card.Text;
                    }
                    Assert.IsTrue(toUser.Text.ToLower().Contains("are you sure"));

                    toBot.Text = "yes";
                    toUser     = await GetResponse(container, MakeRoot, toBot);

                    Assert.IsTrue(toUser.Text.ToLower().Contains("reset count"));

                    //send a random message and check count
                    toBot.Text = "test";
                    toUser     = await GetResponse(container, MakeRoot, toBot);

                    Assert.IsTrue(toUser.Text.StartsWith("1"));

                    toBot.Text = "/deleteprofile";
                    toUser     = await GetResponse(container, MakeRoot, toBot);

                    Assert.IsTrue(toUser.Text.ToLower().Contains("deleted"));
                    using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
                    {
                        var botData = scope.Resolve <IBotData>();
                        await botData.LoadAsync(default(CancellationToken));

                        var stack = scope.Resolve <IDialogStack>();
                        Assert.AreEqual(0, stack.Frames.Count);
                    }
                }
        }