public void EditPost_WithNotSuccessfullEdit_ShouldReturnBadRequest()
        {
            // Arranges
            Mock <IJournalService> journalService = new Mock <IJournalService>();

            JournalFormServiceModel formModel = this.GetJournalFormModel();

            journalService
            .Setup(s => s.Exists(It.IsAny <string>()))
            .Returns(false);

            journalService
            .Setup(s => s.GetName(It.IsAny <int>()))
            .Returns(formModel.Name);

            journalService
            .Setup(s => s.Edit(
                       It.IsAny <int>(),
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <string>()))
            .Returns(false);

            JournalsController journalsController = new JournalsController(journalService.Object);

            // Act
            IActionResult result = journalsController.Edit(1, formModel);

            // Assert
            Assert.IsType <BadRequestResult>(result);
        }
        public void EditGet_WithExistingJournalId_ShouldRetunView()
        {
            // Arrange
            Mock <IJournalService>  journalService = new Mock <IJournalService>();
            JournalFormServiceModel formModel      = this.GetJournalFormModel();

            journalService
            .Setup(s => s.GetForm(It.IsAny <int>()))
            .Returns(formModel);

            JournalsController journalsController = new JournalsController(journalService.Object);

            journalsController.TempData = Mock.Of <ITempDataDictionary>();

            // Act
            IActionResult result = journalsController.Edit(1);

            // Assert
            Assert.IsType <ViewResult>(result);
            object model = (result as ViewResult).Model;

            Assert.IsType <JournalFormServiceModel>(model);
            JournalFormServiceModel returnModel = model as JournalFormServiceModel;

            this.AssertJournals(formModel, returnModel);
        }
        public void EditPost_WithSuccessfullEdit_ShouldReturnRedirectResult()
        {
            // Arranges
            Mock <IJournalService>  journalService = new Mock <IJournalService>();
            JournalFormServiceModel formModel      = this.GetJournalFormModel();

            const int journalId = 1;

            journalService
            .Setup(s => s.Exists(It.IsAny <string>()))
            .Returns(false);

            journalService
            .Setup(s => s.GetName(It.IsAny <int>()))
            .Returns(formModel.Name);

            journalService
            .Setup(s => s.Edit(
                       It.IsAny <int>(),
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <string>()))
            .Returns(true);

            Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>();

            string successmessage = null;

            tempData
            .SetupSet(t => t[WebConstants.TempDataSuccessMessage]    = It.IsAny <string>())
            .Callback((string key, object message) => successmessage = message as string);

            JournalsController journalsController = new JournalsController(journalService.Object);

            journalsController.TempData = tempData.Object;

            // Act
            IActionResult result = journalsController.Edit(journalId, formModel);

            // Assert
            Assert.IsType <RedirectToActionResult>(result);
            RedirectToActionResult redirectResult = result as RedirectToActionResult;

            this.AssertRedirect(journalId, redirectResult);
            Assert.Equal(string.Format(WebConstants.SuccessfullEntityOperation, Journal, WebConstants.Edited), successmessage);
        }
        public void EditPost_WithNotExistingJournalId_ShouldRetunBadRequest()
        {
            // Arrange
            Mock <IJournalService>  journalService = new Mock <IJournalService>();
            JournalFormServiceModel formModel      = null;

            journalService
            .Setup(s => s.GetForm(It.IsAny <int>()))
            .Returns(formModel);

            JournalsController journalsController = new JournalsController(journalService.Object);

            journalsController.TempData = Mock.Of <ITempDataDictionary>();

            // Act
            IActionResult result = journalsController.Edit(1);

            // Assert
            Assert.IsType <BadRequestResult>(result);
        }
        public void EditPost_WithExistingJournalName_ShouldReturnView()
        {
            // Arranges
            Mock <IJournalService> journalService = new Mock <IJournalService>();

            journalService
            .Setup(s => s.Exists(It.IsAny <string>()))
            .Returns(true);

            journalService
            .Setup(s => s.GetName(It.IsAny <int>()))
            .Returns("New Name");

            Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>();

            string errorMessage = null;

            tempData
            .SetupSet(t => t[WebConstants.TempDataErrorMessage]    = It.IsAny <string>())
            .Callback((string key, object message) => errorMessage = message as string);

            JournalFormServiceModel formModel          = this.GetJournalFormModel();
            JournalsController      journalsController = new JournalsController(journalService.Object);

            journalsController.TempData = tempData.Object;

            // Act
            IActionResult result = journalsController.Edit(1, formModel);

            // Assert
            Assert.IsType <ViewResult>(result);
            object model = (result as ViewResult).Model;

            Assert.IsType <JournalFormServiceModel>(model);
            JournalFormServiceModel returnModel = model as JournalFormServiceModel;

            this.AssertJournals(formModel, returnModel);
            Assert.Equal(string.Format(WebConstants.EntryExists, Journal), errorMessage);
        }