public void CreatePost_WithExistingJournalName_ShouldReturnView() { // Arrange Mock <IJournalService> journalService = new Mock <IJournalService>(); journalService .Setup(s => s.Exists(It.IsAny <string>())) .Returns(true); 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.Create(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); }
public void CreatePost_WithNotSuccessfullyCreatedJournal_ShouldReturnBadRequest() { // Arrange Mock <IJournalService> journalService = new Mock <IJournalService>(); journalService .Setup(s => s.Exists(It.IsAny <string>())) .Returns(false); journalService .Setup(s => s.Create( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Returns(-1); JournalFormServiceModel formModel = this.GetJournalFormModel(); JournalsController journalsController = new JournalsController(journalService.Object); journalsController.TempData = Mock.Of <ITempDataDictionary>(); // Act IActionResult result = journalsController.Create(formModel); // Assert Assert.IsType <BadRequestResult>(result); }
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 IActionResult Edit(int id, JournalFormServiceModel model) { string oldName = this.journalService.GetName(id); if (oldName == null) { return(BadRequest()); } string newName = model.Name; if (this.journalService.Exists(newName) && oldName != newName) { TempData.AddErrorMessage(string.Format(WebConstants.EntryExists, Journal)); return(View(model)); } bool success = this.journalService.Edit( id, model.Name, model.Description, model.ImageUrl); if (!success) { return(BadRequest()); } TempData.AddSuccessMessage(string.Format(WebConstants.SuccessfullEntityOperation, Journal, WebConstants.Edited)); return(RedirectToAction(Details, Journals, new { area = string.Empty, id })); }
public IActionResult Edit(int id) { JournalFormServiceModel model = this.journalService.GetForm(id); if (model == null) { return(BadRequest()); } return(View(model)); }
public void GetForm_WithNotExistingId_ShouldReturnNull() { // Arrange StarStuffDbContext db = this.Database; JournalService journalService = new JournalService(db); const int journalId = 1; // Act JournalFormServiceModel actual = journalService.GetForm(journalId); // Assert Assert.Null(actual); }
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 GetForm_WithExistingId_ShouldReturnForm() { // Arrange StarStuffDbContext db = this.Database; JournalService journalService = new JournalService(db); this.SeedDatabase(db); const int journalId = 1; Journal expected = this.GetFakeJournals().FirstOrDefault(j => j.Id == journalId); // Act JournalFormServiceModel actual = journalService.GetForm(journalId); // Assert this.CompareJournals(expected, actual); }
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 IActionResult Create(JournalFormServiceModel model) { if (this.journalService.Exists(model.Name)) { TempData.AddErrorMessage(string.Format(WebConstants.EntryExists, Journal)); return(View(model)); } int id = this.journalService.Create( model.Name, model.Description, model.ImageUrl); if (id <= 0) { return(BadRequest()); } TempData.AddSuccessMessage(string.Format(WebConstants.SuccessfullEntityOperation, Journal, WebConstants.Added)); return(RedirectToAction(Details, Journals, new { area = string.Empty, id })); }
private void AssertJournals(JournalFormServiceModel expected, JournalFormServiceModel actual) { Assert.Equal(expected.Name, actual.Name); Assert.Equal(expected.Description, actual.Description); Assert.Equal(expected.ImageUrl, actual.ImageUrl); }