public void ReturnJsonWihtCorrectSuccessMessage_IfAddingLocationComplete()
        {
            // Arrange
            var mockedLocationFactory = new Mock<ILocationFactory>();
            mockedLocationFactory.Setup(f => f.CreateLocation(It.IsAny<double>(), It.IsAny<double>(), It.IsAny<string>(), It.IsAny<string>())).Verifiable();

            var mockedLocationService = new Mock<ILocationService>();
            mockedLocationService.Setup(s => s.Add(It.IsAny<Location>())).Verifiable();
            mockedLocationService.Setup(s => s.Save()).Verifiable();

            var controller = new LocationController(mockedLocationFactory.Object, mockedLocationService.Object);
            var model = new LocationViewModel() { LocationName = "Name", Latitude = 1, Longitude = 1 };

            // Act
            var result = controller.Add(model) as JsonResult;
            dynamic dResult = result.Data;

            // Assert
            Assert.AreEqual(GlobalMessages.AddLocationSuccessMessage, dResult.message);
            Assert.AreEqual("success", dResult.status);

            mockedLocationFactory.Verify(f => f.CreateLocation(It.IsAny<double>(), It.IsAny<double>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);

            mockedLocationService.Verify(s => s.Add(It.IsAny<Location>()), Times.Once);
            mockedLocationService.Verify(s => s.Save(), Times.Once);
        }
        public void ReturnJsonWihtCorrectErrorMessage_IfModelStateIsNotValid()
        {
            // Arrange
            var mockedLocationFactory = new Mock<ILocationFactory>();
            mockedLocationFactory.Setup(f => f.CreateLocation(It.IsAny<double>(), It.IsAny<double>(), It.IsAny<string>())).Verifiable();

            var mockedLocationService = new Mock<ILocationService>();
            mockedLocationService.Setup(s => s.Add(It.IsAny<Location>())).Verifiable();

            var controller = new LocationController(mockedLocationFactory.Object, mockedLocationService.Object);
            var model = new LocationViewModel();
            controller.ModelState.AddModelError("Name", "Error");
            
            // Act
            var result = controller.Add(model) as JsonResult;
            dynamic dResult = result.Data;

            // Assert
            Assert.AreEqual(GlobalMessages.InvalidLocationModelErrorMessage, dResult.message);
            Assert.AreEqual("error", dResult.status);

            mockedLocationFactory.Verify(f => f.CreateLocation(It.IsAny<double>(), It.IsAny<double>(), It.IsAny<string>()), Times.Never);

            mockedLocationService.Verify(s => s.Add(It.IsAny<Location>()), Times.Never);
            mockedLocationService.Verify(s => s.Save(), Times.Never);
        }
        public void RetunrView_WithNameAdd()
        {
            // Arrange
            var mockedLocationFactory = new Mock <ILocationFactory>();
            var mockedLocationService = new Mock <ILocationService>();

            var controller = new LocationController(mockedLocationFactory.Object, mockedLocationService.Object);

            // Act
            var view = controller.Add() as ViewResult;

            // Assert
            Assert.IsTrue(view.ViewName == ""); // If ViewName isn`t empty string, then controller retuns not his default View
        }
Example #4
0
        private int? SaveLocation( Location l )
        {
            LocationController controller = new LocationController( Service );
            if ( l.Id == 0 )
            {
                l.CreatedByPersonAliasId = Service.LoggedInPerson.PrimaryAliasId;
                controller.Add( l );
            }
            else
            {
                l.ModifiedByPersonAliasId = Service.LoggedInPerson.PrimaryAliasId;
                controller.Update( l );
            }

            return controller.GetByGuid( l.Guid ).Id;
        }