Ejemplo n.º 1
0
        public void CreatePost_WithExistingTelescopeName_ShouldReturnView()
        {
            // Arrange
            Mock <ITelescopeService> telescopeService = new Mock <ITelescopeService>();

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

            TelescopeFormServiceModel formModel            = this.GetTelescopeFormModel();
            TelescopesController      telescopesController = new TelescopesController(telescopeService.Object);

            telescopesController.TempData = tempData.Object;

            // Act
            IActionResult result = telescopesController.Create(formModel);

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

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

            this.AssertTelescopes(formModel, returnModel);
            Assert.Equal(string.Format(WebConstants.EntryExists, Telescope), errorMessage);
        }
Ejemplo n.º 2
0
        public void CreatePost_WithNotSuccessfullyCreatedTelescope_ShouldReturnBadRequest()
        {
            // Arrange
            Mock <ITelescopeService> telescopeService = new Mock <ITelescopeService>();

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

            telescopeService
            .Setup(s => s.Create(
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <double>(),
                       It.IsAny <string>()))
            .Returns(-1);

            TelescopeFormServiceModel formModel            = this.GetTelescopeFormModel();
            TelescopesController      telescopesController = new TelescopesController(telescopeService.Object);

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

            // Act
            IActionResult result = telescopesController.Create(formModel);

            // Assert
            Assert.IsType <BadRequestResult>(result);
        }
Ejemplo n.º 3
0
        public void EditPost_WithNotSuccessfullEdit_ShouldReturnBadRequest()
        {
            // Arranges
            Mock <ITelescopeService> telescopeService = new Mock <ITelescopeService>();

            TelescopeFormServiceModel formModel = this.GetTelescopeFormModel();

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

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

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

            TelescopesController telescopesController = new TelescopesController(telescopeService.Object);

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

            // Assert
            Assert.IsType <BadRequestResult>(result);
        }
Ejemplo n.º 4
0
        public void EditGet_WithExistingTelescopeId_ShouldRetunView()
        {
            // Arrange
            Mock <ITelescopeService>  telescopeService = new Mock <ITelescopeService>();
            TelescopeFormServiceModel formModel        = this.GetTelescopeFormModel();

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

            TelescopesController telescopesController = new TelescopesController(telescopeService.Object);

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

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

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

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

            this.AssertTelescopes(formModel, returnModel);
        }
        public IActionResult Edit(int id, TelescopeFormServiceModel model)
        {
            string oldName = this.telescopeService.GetName(id);

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

            string newName = model.Name;

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

            bool success = this.telescopeService.Edit(
                id,
                model.Name,
                model.Location,
                model.Description,
                model.MirrorDiameter,
                model.ImageUrl);

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

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

            return(RedirectToAction(Details, Telescopes, new { area = string.Empty, id }));
        }
Ejemplo n.º 6
0
 private void AssertTelescopes(TelescopeFormServiceModel expected, TelescopeFormServiceModel actual)
 {
     Assert.Equal(expected.Name, actual.Name);
     Assert.Equal(expected.Location, actual.Location);
     Assert.Equal(expected.Description, actual.Description);
     Assert.Equal(expected.MirrorDiameter, actual.MirrorDiameter);
     Assert.Equal(expected.ImageUrl, actual.ImageUrl);
 }
        public IActionResult Edit(int id)
        {
            TelescopeFormServiceModel model = this.telescopeService.GetForm(id);

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

            return(View(model));
        }
        public void GetForm_WithNotExistingId_ShouldReturnNull()
        {
            // Arrange
            StarStuffDbContext db = this.Database;
            TelescopeService   telescopeService = new TelescopeService(db);
            const int          telescopeId      = 1;

            // Act
            TelescopeFormServiceModel result = telescopeService.GetForm(telescopeId);

            // Assert
            Assert.Null(result);
        }
Ejemplo n.º 9
0
        public void EditPost_WithSuccessfullEdit_ShouldReturnRedirectResult()
        {
            // Arranges
            Mock <ITelescopeService>  telescopeService = new Mock <ITelescopeService>();
            TelescopeFormServiceModel formModel        = this.GetTelescopeFormModel();

            const int telescopeId = 1;

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

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

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

            TelescopesController telescopesController = new TelescopesController(telescopeService.Object);

            telescopesController.TempData = tempData.Object;

            // Act
            IActionResult result = telescopesController.Edit(telescopeId, formModel);

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

            this.AssertRedirect(telescopeId, redirectResult);
            Assert.Equal(string.Format(WebConstants.SuccessfullEntityOperation, Telescope, WebConstants.Edited), successmessage);
        }
Ejemplo n.º 10
0
        public void GetForm_WithExistingId_ShouldReturnTelescope()
        {
            // Arrange
            StarStuffDbContext db = this.Database;
            TelescopeService   telescopeService = new TelescopeService(db);

            this.SeedDatabase(db);

            const int telescopeId = 1;

            Telescope expected = this.GetFakeTelescopes().FirstOrDefault(t => t.Id == telescopeId);

            // Act
            TelescopeFormServiceModel actual = telescopeService.GetForm(telescopeId);

            // Assert
            this.CompareTelescopes(expected, actual);
        }
Ejemplo n.º 11
0
        public void EditPost_WithNotExistingTelescopeId_ShouldRetunBadRequest()
        {
            // Arrange
            Mock <ITelescopeService>  telescopeService = new Mock <ITelescopeService>();
            TelescopeFormServiceModel formModel        = null;

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

            TelescopesController telescopesController = new TelescopesController(telescopeService.Object);

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

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

            // Assert
            Assert.IsType <BadRequestResult>(result);
        }
Ejemplo n.º 12
0
        public IActionResult Create(TelescopeFormServiceModel model)
        {
            if (this.telescopeService.Exists(model.Name))
            {
                TempData.AddErrorMessage(string.Format(WebConstants.EntryExists, Telescope));
                return(View(model));
            }

            int id = this.telescopeService.Create(
                model.Name,
                model.Location,
                model.Description,
                model.MirrorDiameter,
                model.ImageUrl);

            if (id <= 0)
            {
                return(BadRequest());
            }

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

            return(RedirectToAction(Details, Telescopes, new { area = string.Empty, id }));
        }