public async Task PrivateChatWithPushedMiddleware() { var nextHandler = Substitute.For <NextHandler>(); var privateChatService = new PrivateChatService(); var middleware = new PrivateChatMiddleware(privateChatService); var otherMiddleware = Substitute.For <IMessageMiddleware>(); otherMiddleware.HandleMessageAsync(default !, default !).ReturnsForAnyArgs(
public async Task Test() { var container = new Container(); container.Register <PrivateChatService>(Reuse.Singleton); var privateChatMiddleware = new PrivateChatMiddleware(container.Resolve <PrivateChatService>()); var messageDispatcher = new MessageDispatcher(container, Substitute.For <IBotService>()); var commandService = new CommandService(container, messageDispatcher); commandService.RegisterCommand <TestCommand>(); var response = privateChatMiddleware.HandleMessageAsync(new Message() { Chat = new Chat() { Id = 1234, Type = ChatType.Private }, From = new User() { Id = 1234 }, Text = "/test", Entities = new[] { new MessageEntity() { Type = MessageEntityType.BotCommand, Offset = 0, Length = 5, }, }, }, message => messageDispatcher.HandleMessageAsync(message !, Substitute.For <NextHandler>())).GetAsyncEnumerator(); Assert.True(await response.MoveNextAsync()); Assert.Equal("Start prompting...", response.Current.Content); var userInputTask = response.MoveNextAsync().AsTask(); await privateChatMiddleware.HandleMessageAsync(new Message() { Chat = new Chat() { Id = 1234, Type = ChatType.Private }, From = new User() { Id = 1234 }, Text = "I am typing.", }, Substitute.For <NextHandler>()).GetAsyncEnumerator().MoveNextAsync(); Assert.True(await userInputTask); Assert.Equal("You've typed 'I am typing.'", response.Current.Content); }
public async Task NonPrivateChat() { var nextHandler = Substitute.For <NextHandler>(); var middleware = new PrivateChatMiddleware(new PrivateChatService()); var message = new Message() { Chat = new Chat() { Id = -1234, Type = ChatType.Group }, }; var response = middleware.HandleMessageAsync(message, nextHandler).GetAsyncEnumerator(); Assert.False(await response.MoveNextAsync()); nextHandler.Received().Invoke(message); }