public void CreatePost_WithExistingStarName_ShouldReturnView()
        {
            // Arrange
            Mock <IStarService> starService = new Mock <IStarService>();

            starService
            .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);

            StarFormServiceModel formModel       = this.GetStarFormModel();
            StarsController      starsController = new StarsController(starService.Object, null);

            starsController.TempData = tempData.Object;

            // Act
            IActionResult result = starsController.Create(1, formModel);

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

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

            this.AssertStars(formModel, returnModel);
            Assert.Equal(string.Format(WebConstants.EntryExists, Star), errorMessage);
        }
        public void CreatePost_WithNotSuccessfullyCreatedStar_ShouldReturnBadRequest()
        {
            // Arrange
            Mock <IStarService>      starService      = new Mock <IStarService>();
            Mock <IDiscoveryService> discoveryService = new Mock <IDiscoveryService>();

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

            discoveryService
            .Setup(d => d.TotalStars(It.IsAny <int>()))
            .Returns(1);

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

            StarsController starsController = new StarsController(starService.Object, discoveryService.Object);

            starsController.TempData = Mock.Of <ITempDataDictionary>();
            StarFormServiceModel formModel = this.GetStarFormModel();

            // Act
            IActionResult result = starsController.Create(1, formModel);

            // Assert
            Assert.IsType <BadRequestResult>(result);
        }
        public void CreateGet_ShouldReturnView()
        {
            // Arrange
            StarsController starsController = new StarsController(null, null);

            // Act
            IActionResult result = starsController.Create();

            // Assert
            Assert.IsType <ViewResult>(result);
        }
        public void CreatePost_WithSuccessfullyCreatedStar_ShouldReturnRedirectResult()
        {
            // Arrange
            Mock <IStarService>      starService      = new Mock <IStarService>();
            Mock <IDiscoveryService> discoveryService = new Mock <IDiscoveryService>();

            const int discoveryId = 1;

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

            discoveryService
            .Setup(d => d.TotalStars(It.IsAny <int>()))
            .Returns(1);

            starService
            .Setup(s => s.Create(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <int>()))
            .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);

            StarFormServiceModel formModel       = this.GetStarFormModel();
            StarsController      starsController = new StarsController(starService.Object, discoveryService.Object);

            starsController.TempData = tempData.Object;

            // Act
            IActionResult result = starsController.Create(discoveryId, formModel);

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

            this.AssertRedirect(discoveryId, redirectResult);
            Assert.Equal(string.Format(WebConstants.SuccessfullEntityOperation, Star, WebConstants.Added), successmessage);
        }
        public void CreatePost_WithDiscoveryWithMoreThanThreeStars_ShouldReturnView()
        {
            // Arrange
            Mock <IStarService>      starService      = new Mock <IStarService>();
            Mock <IDiscoveryService> discoveryService = new Mock <IDiscoveryService>();

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

            discoveryService
            .Setup(d => d.TotalStars(It.IsAny <int>()))
            .Returns(3);

            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);

            StarFormServiceModel formModel       = this.GetStarFormModel();
            StarsController      starsController = new StarsController(starService.Object, discoveryService.Object);

            starsController.TempData = tempData.Object;

            // Act
            IActionResult result = starsController.Create(1, formModel);

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

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

            this.AssertStars(formModel, returnModel);
            Assert.Equal(string.Format(
                             DataConstants.DiscoveryConstants.MaxStarsPerDiscoveryErrorMessage,
                             DataConstants.DiscoveryConstants.MaxStarsPerDiscovery),
                         errorMessage);
        }