public void SetUp()
 {
     markdown = new MarkdownDeep.Markdown();
     message1 = new MessageViewModel { AuthorName = "Dianne", ID = 1, RoomID = 12345, Text = "Test!" };
     mockRoomViewModelReader = new Mock<IRoomViewModelReader>();
     var roomMessages = new[] { message1 };
     mockRoomViewModelReader.Setup(reader => reader.GetRoomMessages(It.IsAny<int>())).Returns(roomMessages);
 }
        public ActionResult AddMessage(MessageViewModel messageViewModel)
        {
            ActionResult result;

            if (ModelState.IsValid)
            {
                writer.AddMessage(messageViewModel);
                result = Json(messageViewModel);
            }
            else
            {
                result = new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            return result;
        }
 public void AddMessage(MessageViewModel messageViewModel)
 {
     var messageRecord = mapper.MapMessageViewModelToMessageRecord(messageViewModel);
     messageRepository.AddMessageToRoom(messageRecord.RoomID, messageRecord.AuthorName, messageRecord.Text);
 }
        public void PostAddMessageWithInvalidTextReturnsBadRequestStatusCode(string text)
        {
            var controller = CreateController();
            var viewModel = new MessageViewModel { AuthorName = "David", Text = text };
            controller.ViewData.ModelState.AddModelError("Message Text", "The message text field is required.");

            var result = controller.AddMessage(viewModel);

            Assert.That(result, Is.InstanceOf<HttpStatusCodeResult>());

            var httpStatusCodeResult = result as HttpStatusCodeResult;
            Assert.That(httpStatusCodeResult.StatusCode, Is.EqualTo(400));
        }
        public void PostAddMessageWithInvalidTextCausesValidationError(string text)
        {
            var controller = CreateController();

            var viewModel = new MessageViewModel { AuthorName = "David", Text = text };

            var context = new ValidationContext(viewModel, serviceProvider: null, items: null);
            var results = new List<ValidationResult>();

            var isValid = Validator.TryValidateObject(viewModel, context, results);

            Assert.That(isValid, Is.False);
        }
        public void PostAddMessageReturnsJsonResult()
        {
            var controller = CreateController();

            var viewModel = new MessageViewModel { AuthorName = "David", Text = "Hello!" };

            var result = controller.AddMessage(viewModel);

            Assert.That(result, Is.InstanceOf<JsonResult>());
        }
        public void PostAddMessageDelegatesToWriter()
        {
            var controller = CreateController();

            var viewModel = new MessageViewModel { AuthorName = "David", Text = "Hello!" };

            controller.AddMessage(viewModel);

            mockWriter.Verify(writer => writer.AddMessage(viewModel));
        }
 public MessageRecord MapMessageViewModelToMessageRecord(MessageViewModel messageViewModel)
 {
     return new MessageRecord(messageViewModel.RoomID, messageViewModel.AuthorName, messageViewModel.Text);
 }
 public void AddMessage(MessageViewModel messageViewModel)
 {
     var messagesCollection = GetMessagesCollection();
     messagesCollection.Save(messageViewModel);
 }