public void CreatePost_WithNotValidModelState_ShouldReturnCorrectViewModel() { // Arrange Mock <IDiscoveryService> discoveryService = new Mock <IDiscoveryService>(); Mock <IJournalService> journalService = new Mock <IJournalService>(); PublicationFormViewModel formModel = this.GetPublicationFormViewModel(); const int journalId = 1; discoveryService .Setup(d => d.DiscoveryDropdown(It.IsAny <int>())) .Returns(this.GetDiscoveriesDropdown()); journalService .Setup(j => j.GetName(It.IsAny <int>())) .Returns(formModel.JournalName); PublicationsController publicationsController = new PublicationsController(null, discoveryService.Object, journalService.Object, null); publicationsController.ModelState.AddModelError(string.Empty, "Error"); // Act IActionResult result = publicationsController.Create(journalId, formModel); // Assert Assert.IsType <ViewResult>(result); object model = (result as ViewResult).Model; Assert.IsType <PublicationFormViewModel>(model); PublicationFormViewModel returnModel = model as PublicationFormViewModel; this.AssertPublicationFormViewModel(formModel, returnModel); }
public void CreateGet_ShouldReturnCorrectViewModel() { // Arrange Mock <IDiscoveryService> discoveryService = new Mock <IDiscoveryService>(); Mock <IJournalService> journalService = new Mock <IJournalService>(); PublicationFormViewModel formModel = this.GetPublicationFormViewModel(); const int journalId = 1; discoveryService .Setup(d => d.DiscoveryDropdown(It.IsAny <int>())) .Returns(this.GetDiscoveriesDropdown()); journalService .Setup(j => j.GetName(It.IsAny <int>())) .Returns(formModel.JournalName); PublicationsController publicationsController = new PublicationsController(null, discoveryService.Object, journalService.Object, null); // Act IActionResult result = publicationsController.Create(journalId); // Assert Assert.IsType <ViewResult>(result); object model = (result as ViewResult).Model; Assert.IsType <PublicationFormViewModel>(model); PublicationFormViewModel returnModel = model as PublicationFormViewModel; Assert.Equal(formModel.JournalName, returnModel.JournalName); this.AssertDiscoveriesSelectList(returnModel.Discoveries); }
public void CreatePost_WithExistingPublicationFromJournalForDiscovery_ShouldReturnCorrectViewModel() { // Arrange Mock <IDiscoveryService> discoveryService = new Mock <IDiscoveryService>(); Mock <IJournalService> journalService = new Mock <IJournalService>(); Mock <IPublicationService> publicationService = new Mock <IPublicationService>(); Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); PublicationFormViewModel formModel = this.GetPublicationFormViewModel(); const int journalId = 1; const string discoveryName = "Fake Discovery Name"; discoveryService .Setup(d => d.DiscoveryDropdown(It.IsAny <int>())) .Returns(this.GetDiscoveriesDropdown()); discoveryService .Setup(d => d.GetName(It.IsAny <int>())) .Returns(discoveryName); journalService .Setup(j => j.GetName(It.IsAny <int>())) .Returns(formModel.JournalName); publicationService .Setup(p => p.TitleExists(It.IsAny <string>())) .Returns(false); publicationService .Setup(p => p.Exists(It.IsAny <int>(), It.IsAny <int>())) .Returns(true); string errorMessage = null; tempData .SetupSet(t => t[WebConstants.TempDataErrorMessage] = It.IsAny <string>()) .Callback((string key, object message) => errorMessage = message as string); PublicationsController publicationsController = new PublicationsController(publicationService.Object, discoveryService.Object, journalService.Object, null); publicationsController.TempData = tempData.Object; // Act IActionResult result = publicationsController.Create(journalId, formModel); // Assert Assert.IsType <ViewResult>(result); object model = (result as ViewResult).Model; Assert.IsType <PublicationFormViewModel>(model); PublicationFormViewModel returnModel = model as PublicationFormViewModel; this.AssertPublicationFormViewModel(formModel, returnModel); Assert.Equal(string.Format(WebConstants.PublicationFromJournalExists, formModel.JournalName, discoveryName), errorMessage); }
public void CreatePost_WithSuccessfullyCreatedPublication_ShouldReturnRedirectResult() { // Arrange Mock <IPublicationService> publicationService = new Mock <IPublicationService>(); Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); Mock <UserManager <User> > userManager = UserManagerMock.New; const int journalId = 1; publicationService .Setup(p => p.TitleExists(It.IsAny <string>())) .Returns(false); publicationService .Setup(p => p.Exists(It.IsAny <int>(), It.IsAny <int>())) .Returns(false); publicationService .Setup(p => p.Create( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>())) .Returns(journalId); userManager .Setup(u => u.GetUserId(It.IsAny <ClaimsPrincipal>())) .Returns("1"); string successmessage = null; tempData .SetupSet(t => t[WebConstants.TempDataSuccessMessage] = It.IsAny <string>()) .Callback((string key, object message) => successmessage = message as string); PublicationFormViewModel formModel = this.GetPublicationFormViewModel(); PublicationsController publicationsController = new PublicationsController(publicationService.Object, null, null, userManager.Object); publicationsController.TempData = tempData.Object; // Act IActionResult result = publicationsController.Create(journalId, formModel); // Assert Assert.IsType <RedirectToActionResult>(result); RedirectToActionResult redirectResult = result as RedirectToActionResult; this.AssertRedirect(journalId, redirectResult); Assert.Equal(string.Format(WebConstants.SuccessfullEntityOperation, Publication, WebConstants.Added), successmessage); }
public void CreatePost_WithNotSuccessfullyCreatedPublication_ShouldReturnBadRequest() { // Arrange Mock <IJournalService> journalService = new Mock <IJournalService>(); Mock <IPublicationService> publicationService = new Mock <IPublicationService>(); Mock <UserManager <User> > userManager = UserManagerMock.New; const int journalId = 1; publicationService .Setup(p => p.TitleExists(It.IsAny <string>())) .Returns(false); publicationService .Setup(p => p.Exists(It.IsAny <int>(), It.IsAny <int>())) .Returns(false); publicationService .Setup(p => p.Create( It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>())) .Returns(-1); userManager .Setup(u => u.GetUserId(It.IsAny <ClaimsPrincipal>())) .Returns("1"); PublicationFormViewModel formModel = this.GetPublicationFormViewModel(); PublicationsController publicationsController = new PublicationsController(publicationService.Object, null, null, userManager.Object); // Act IActionResult result = publicationsController.Create(journalId, formModel); // Assert Assert.IsType <BadRequestResult>(result); }