public void CreatePost_WithExistingPlanetName_ShouldReturnView()
        {
            // Arrange
            Mock <IPlanetService> planetService = new Mock <IPlanetService>();

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

            PlanetFormServiceModel formModel         = this.GetPlanetFormModel();
            PlanetsController      planetsController = new PlanetsController(planetService.Object);

            planetsController.TempData = tempData.Object;

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

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

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

            this.AssertPlanets(formModel, returnModel);
            Assert.Equal(string.Format(WebConstants.EntryExists, Planet), errorMessage);
        }
        public IActionResult Edit(int id, int discoveryId, PlanetFormServiceModel model)
        {
            string oldName = this.planetService.GetName(id);

            if (oldName == null)
            {
                return(BadRequest());
            }

            string newName = model.Name;

            if (this.planetService.Exists(newName) &&
                oldName != newName)
            {
                TempData.AddErrorMessage(string.Format(WebConstants.EntryExists, Planet));
                return(View(model));
            }

            bool success = this.planetService.Edit(id, model.Name, model.Mass);

            if (!success)
            {
                return(BadRequest());
            }

            TempData.AddSuccessMessage(string.Format(WebConstants.SuccessfullEntityOperation, Planet, WebConstants.Edited));

            return(RedirectToAction(nameof(DiscoveriesController.Details), Discoveries, new { id = discoveryId, area = WebConstants.AstronomerArea }));
        }
        public void EditPost_WithNotSuccessfullEdit_ShouldReturnBadRequest()
        {
            // Arranges
            Mock <IPlanetService> planetService = new Mock <IPlanetService>();

            PlanetFormServiceModel formModel = this.GetPlanetFormModel();

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

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

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

            PlanetsController planetsController = new PlanetsController(planetService.Object);

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

            // Assert
            Assert.IsType <BadRequestResult>(result);
        }
        public void EditGet_WithExistingId_ShouldReturnView()
        {
            // Arrange
            Mock <IPlanetService>  planetService = new Mock <IPlanetService>();
            PlanetFormServiceModel formModel     = this.GetPlanetFormModel();

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

            PlanetsController planetsController = new PlanetsController(planetService.Object);

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

            // Assert

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

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

            this.AssertPlanets(formModel, returnModel);
        }
        public IActionResult Edit(int id, int discoveryId)
        {
            PlanetFormServiceModel model = this.planetService.GetForm(id);

            if (model == null)
            {
                return(BadRequest());
            }

            return(View(model));
        }
Exemple #6
0
        public void GetForm_WithNotExistingId_ShouldReturnNull()
        {
            // Arrange
            StarStuffDbContext db            = this.Database;
            PlanetService      planetService = new PlanetService(db);

            this.SeedDatabase(db);

            // Act
            PlanetFormServiceModel actual = planetService.GetForm(11);

            // Assert
            Assert.Null(actual);
        }
Exemple #7
0
        public void GetForm_WithExistingId_ShouldReturnCorrectResult()
        {
            // Arrange
            StarStuffDbContext db            = this.Database;
            PlanetService      planetService = new PlanetService(db);

            this.SeedDatabase(db);
            Planet expected = this.GetFakePlanets().First();

            // Act
            PlanetFormServiceModel actual = planetService.GetForm(expected.Id);

            // Assert
            this.ComparePlanets(expected, actual);
        }
        public void EditGet_WithNotExistingId_ShouldReturnBadRequest()
        {
            // Arrange
            Mock <IPlanetService>  planetService = new Mock <IPlanetService>();
            PlanetFormServiceModel model         = null;

            planetService
            .Setup(s => s.GetForm(It.IsAny <int>()))
            .Returns(model);

            PlanetsController planetsController = new PlanetsController(planetService.Object);

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

            // Assert
            Assert.IsType <BadRequestResult>(result);
        }
        public IActionResult Create(int id, PlanetFormServiceModel model)
        {
            if (this.planetService.Exists(model.Name))
            {
                TempData.AddErrorMessage(string.Format(WebConstants.EntryExists, Planet));
                return(View(model));
            }

            bool success = this.planetService.Create(id, model.Name, model.Mass);

            if (!success)
            {
                return(BadRequest());
            }

            TempData.AddSuccessMessage(string.Format(WebConstants.SuccessfullEntityOperation, Planet, WebConstants.Added));

            return(RedirectToAction(nameof(DiscoveriesController.Details), Discoveries, new { id, area = WebConstants.AstronomerArea }));
        }
        public void EditPost_WithSuccessfullEdit_ShouldReturnRedirectResult()
        {
            // Arranges
            Mock <IPlanetService>  planetService = new Mock <IPlanetService>();
            PlanetFormServiceModel formModel     = this.GetPlanetFormModel();

            const int discoveryId = 1;

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

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

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

            PlanetsController planetsController = new PlanetsController(planetService.Object);

            planetsController.TempData = tempData.Object;

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

            // Assert

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

            this.AssertRedirect(discoveryId, redirectResult);
            Assert.Equal(string.Format(WebConstants.SuccessfullEntityOperation, Planet, WebConstants.Edited), successmessage);
        }
        public void CreatePost_WithNotSuccessfullyCreatedPlanet_ShouldReturnBadRequest()
        {
            // Arrange
            Mock <IPlanetService> planetService = new Mock <IPlanetService>();

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

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

            PlanetsController planetsController = new PlanetsController(planetService.Object);

            planetsController.TempData = Mock.Of <ITempDataDictionary>();
            PlanetFormServiceModel formModel = this.GetPlanetFormModel();

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

            // Assert
            Assert.IsType <BadRequestResult>(result);
        }
Exemple #12
0
 private void ComparePlanets(Planet expected, PlanetFormServiceModel actual)
 {
     Assert.Equal(expected.Mass, actual.Mass);
     Assert.Equal(expected.Name, actual.Name);
 }