예제 #1
0
        public async Task PostNewMessagesCallsTheRepositoryInsertMethod(
            Mock<IUsernameProvider> usernameProvider,
            Mock<IRepository<ChatMessageDataModel>> messageRepository,
            string hubId,
            string channelId,
            string username,
            NewMessageModel newMessage,
            ChatMessageDataModel createdMessage)
        {
            AutoMapper.Mapper.CreateMap<ChatMessage, ChatMessageDataModel>();

            usernameProvider.Setup(p => p.GetUsername()).Returns(Task.FromResult(username));
            messageRepository.Setup(r => r.Insert(It.IsAny<ChatMessageDataModel>())).Returns(Task.FromResult(createdMessage));

            var controllerUnderTest = new MessagesController(usernameProvider.Object, messageRepository.Object);

            await controllerUnderTest.PostNewMessage(hubId, channelId, newMessage);

            var expectedMessageParameter = new ChatMessage(username, channelId, newMessage.Message, DateTime.Now);
            
            messageRepository.Verify(mock => mock.Insert(It.Is<ChatMessageDataModel>(parameter =>
                parameter.ChannelId == channelId && 
                parameter.Message == newMessage.Message &&
                parameter.Username == username
            )), Times.Once());
        }
예제 #2
0
        public async Task<IActionResult> PostNewMessage(string hubSlug, string channelId, [FromBody] NewMessageModel messageContent)
        {
            var username = await this.usernameProvider.GetUsername();

            var message = new ChatMessage(username, channelId, messageContent.Message, DateTime.Now);

            var dto = AutoMapper.Mapper.Map<ChatMessage, ChatMessageDataModel>(message);

            dto = await this.messageRepository.Insert(dto);
            
            return new CreatedAtRouteResult(string.Format("/api/{0}/{1}/messages/", hubSlug, channelId), dto);
        }