コード例 #1
0
        public async Task HandleAsync_calls_message_handler_when_update_type_is_Message()
        {
            var fakeLogger         = new Mock <ILogger <TelegramUpdateHandler> >();
            var fakeMessageHandler = new Mock <IHandleTelegramMessage>();
            var fakeOptions        = new Mock <IOptions <TelegramBotConfiguration> >();
            var fakeBotService     = new Mock <ITelegramBotService>();

            var handler = new TelegramUpdateHandler(
                fakeLogger.Object,
                fakeMessageHandler.Object,
                fakeBotService.Object,
                fakeOptions.Object);

            await handler.HandleAsync(TelegramUpdateFactory.CreateFakeTelegramUpdate(UpdateType.Message));

            fakeMessageHandler.Verify(x => x.HandleAsync(It.IsAny <TelegramMessage>()), Times.Once);
        }
コード例 #2
0
        public async Task HandleAsync_should_do_nothing_if_update_is_null()
        {
            var fakeLogger         = new Mock <ILogger <TelegramUpdateHandler> >();
            var fakeMessageHandler = new Mock <IHandleTelegramMessage>();
            var fakeOptions        = new Mock <IOptions <TelegramBotConfiguration> >();
            var fakeBotService     = new Mock <ITelegramBotService>();

            var handler = new TelegramUpdateHandler(
                fakeLogger.Object,
                fakeMessageHandler.Object,
                fakeBotService.Object,
                fakeOptions.Object);

            await handler.HandleAsync(null);

            fakeBotService.VerifyNoOtherCalls();
            fakeMessageHandler.VerifyNoOtherCalls();
        }
コード例 #3
0
        public async Task HandleAsync_does_not_report_or_catch_exception_if_message_handler_fails()
        {
            var fakeLogger         = new Mock <ILogger <TelegramUpdateHandler> >();
            var fakeMessageHandler = new Mock <IHandleTelegramMessage>();
            var fakeOptions        = new Mock <IOptions <TelegramBotConfiguration> >();
            var fakeBotService     = new Mock <ITelegramBotService>();

            var exception = new Exception("whoops");

            fakeMessageHandler.Setup(x => x.HandleAsync(It.IsAny <TelegramMessage>()))
            .ThrowsAsync(exception);

            var fakeClient = new Mock <ITelegramBotClient>();

            fakeBotService.SetupGet(x => x.Client).Returns(fakeClient.Object);

            var fakeConfig = new TelegramBotConfiguration
            {
                EnableExceptionForwarding = false,
                ExceptionChatId           = 42
            };

            fakeOptions.SetupGet(x => x.Value).Returns(fakeConfig);

            var handler = new TelegramUpdateHandler(
                fakeLogger.Object,
                fakeMessageHandler.Object,
                fakeBotService.Object,
                fakeOptions.Object);


            Func <Task> action = async() => await handler.HandleAsync(TelegramUpdateFactory.CreateFakeTelegramUpdate(UpdateType.Message));

            await action.Should().ThrowAsync <Exception>();

            fakeClient.VerifyNoOtherCalls();
        }
コード例 #4
0
        public async Task HandleAsync_catches_and_reports_exception_when_message_handler_throws_and_exception_forwarding_is_enabled()
        {
            var fakeLogger         = new Mock <ILogger <TelegramUpdateHandler> >();
            var fakeMessageHandler = new Mock <IHandleTelegramMessage>();
            var fakeOptions        = new Mock <IOptions <TelegramBotConfiguration> >();
            var fakeBotService     = new Mock <ITelegramBotService>();

            var exception = new Exception("whoops");

            fakeMessageHandler.Setup(x => x.HandleAsync(It.IsAny <TelegramMessage>()))
            .ThrowsAsync(exception);

            var fakeClient = new Mock <ITelegramBotClient>();

            fakeBotService.SetupGet(x => x.Client).Returns(fakeClient.Object);

            var fakeConfig = new TelegramBotConfiguration
            {
                EnableExceptionForwarding = true,
                ExceptionChatId           = 42
            };

            fakeOptions.SetupGet(x => x.Value).Returns(fakeConfig);

            var handler = new TelegramUpdateHandler(
                fakeLogger.Object,
                fakeMessageHandler.Object,
                fakeBotService.Object,
                fakeOptions.Object);

            await handler.HandleAsync(TelegramUpdateFactory.CreateFakeTelegramUpdate(UpdateType.Message));

            var expectedMessage = $"```\n{exception}\n```";

            fakeClient.VerifySendTextMessageAsync(fakeConfig.ExceptionChatId, expectedMessage, Times.Once(), ParseMode.Markdown);
        }