public void InvokeAdvertServiceMethod_Update_Once_WhennModelStateIsValid()
        {
            // Arrange
            var advertViewModel = new AdvertViewModel()
            {
                Title            = "advert",
                VehicleModelId   = 1,
                UserId           = "user id",
                Year             = 100,
                Price            = 100m,
                Power            = 100,
                DistanceCoverage = 100,
                CityId           = 1,
                Description      = "ani description",
                CreatedOn        = DateTime.Now
            };

            var advertService = new Mock <IAdvertService>();

            advertService.Setup(x => x.GetById(It.IsAny <int>())).Returns(new Advert());

            var ajaxAdvertController = new CarAdverts.Web.Areas.Administrator.Controllers.AjaxAdvertController(advertService.Object);

            // Act
            ajaxAdvertController.Update(advertViewModel);

            // Assert
            advertService.Verify(a => a.Update(It.IsAny <Advert>()), Times.Once);
        }
        public void ThrowArgumentNullException_WhenModelParameterIsNull()
        {
            // Arrange
            var advertService = new Mock <IAdvertService>();

            // Act
            var ajaxAdvertController = new CarAdverts.Web.Areas.Administrator.Controllers.AjaxAdvertController(advertService.Object);

            // Arrange
            Assert.Throws <ArgumentNullException>(() => ajaxAdvertController.Update(null));
        }
Esempio n. 3
0
        public void ReturnJsonResultWithCorrectModel()
        {
            // Arrange
            var advertService = new Mock <IAdvertService>();

            var ajaxAdvertController = new CarAdverts.Web.Areas.Administrator.Controllers.AjaxAdvertController(advertService.Object);

            // Act and Arrange
            ajaxAdvertController
            .WithCallTo(x => x.GetById(1));
        }
        public void CreateInstanceOfAjaxAdvertController_WhenAdvertServiceParameterIsNotNull()
        {
            // Arrange
            var advertService = new Mock <IAdvertService>();

            // Act
            var ajaxAdvertController = new CarAdverts.Web.Areas.Administrator.Controllers.AjaxAdvertController(advertService.Object);

            // Act and Assert
            Assert.That(ajaxAdvertController, Is.Not.Null);
            Assert.IsInstanceOf <CarAdverts.Web.Areas.Administrator.Controllers.AjaxAdvertController>(ajaxAdvertController);
        }
Esempio n. 5
0
        public void InvokeAdvertServiceMethod_GetById_Once()
        {
            // Arrange
            var advertService        = new Mock <IAdvertService>();
            var ajaxAdvertController = new CarAdverts.Web.Areas.Administrator.Controllers.AjaxAdvertController(advertService.Object);

            // Act
            ajaxAdvertController.GetById(1);

            // Arrange
            advertService.Verify(x => x.GetById(It.IsAny <int>()), Times.Once);
        }
Esempio n. 6
0
        public void ReturnDefalutView()
        {
            // Arrange
            var advertService = new Mock <IAdvertService>();

            // Act
            var ajaxAdvertController = new CarAdverts.Web.Areas.Administrator.Controllers.AjaxAdvertController(advertService.Object);

            // Arrange
            ajaxAdvertController
            .WithCallTo(x => x.Index())
            .ShouldRenderDefaultView();
        }
        public void InvokeAdvertServiceMethod_Delete_Once()
        {
            // Arrange
            var advertService = new Mock <IAdvertService>();

            advertService.Setup(x => x.GetById(It.IsAny <int>())).Returns(new Advert());

            var ajaxAdvertController = new CarAdverts.Web.Areas.Administrator.Controllers.AjaxAdvertController(advertService.Object);

            // Act
            ajaxAdvertController.Delete(1);

            // Assert
            advertService.Verify(a => a.Delete(It.IsAny <Advert>()), Times.Once);
        }
        public void RedirectToActionIndex_WhennModelStateIsNotValid()
        {
            // Arrange
            var advertService = new Mock <IAdvertService>();

            // Act
            var ajaxAdvertController = new CarAdverts.Web.Areas.Administrator.Controllers.AjaxAdvertController(advertService.Object);

            ajaxAdvertController.ModelState.AddModelError("test", "test");

            // Arrange
            ajaxAdvertController
            .WithCallTo(x => x.Update(new AdvertViewModel()))
            .ShouldRedirectTo(x => x.Index);
        }
        public void ReturnJsonWithCorrectModel_WhennModelStateIsValid()
        {
            // Arrange
            // This code mock User.Identity.GetUserId()
            var context  = new Mock <HttpContextBase>();
            var identity = new GenericIdentity("*****@*****.**");

            identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", "1"));
            var principal = new GenericPrincipal(identity, new[] { "user" });

            context.Setup(s => s.User).Returns(principal);
            //

            var advertViewModel = new AdvertViewModel()
            {
                Title            = "advert",
                VehicleModelId   = 1,
                Year             = 100,
                Price            = 100m,
                Power            = 100,
                DistanceCoverage = 100,
                CityId           = 1,
                Description      = "ani description",
                CreatedOn        = DateTime.Now
            };

            var advertService        = new Mock <IAdvertService>();
            var ajaxAdvertController = new CarAdverts.Web.Areas.Administrator.Controllers.AjaxAdvertController(advertService.Object);

            ajaxAdvertController.ControllerContext = new ControllerContext(context.Object, new RouteData(), ajaxAdvertController);

            // Act and Arrange
            ajaxAdvertController
            .WithCallTo(x => x.Add(advertViewModel))
            .ShouldReturnJson(data =>
            {
                Assert.AreEqual(data.Title, advertViewModel.Title);
                Assert.AreEqual(data.VehicleModelId, advertViewModel.VehicleModelId);
                Assert.AreEqual(data.Year, advertViewModel.Year);
                Assert.AreEqual(data.Price, advertViewModel.Price);
                Assert.AreEqual(data.Power, advertViewModel.Power);
                Assert.AreEqual(data.DistanceCoverage, advertViewModel.DistanceCoverage);
                Assert.AreEqual(data.CityId, advertViewModel.CityId);
                Assert.AreEqual(data.Description, advertViewModel.Description);
                Assert.AreEqual(data.CreatedOn, advertViewModel.CreatedOn);
            });
        }
        public void ReturnJsonWithCorrectModel_WhennModelStateIsValid()
        {
            // Arrange

            var advertViewModel = new AdvertViewModel()
            {
                Title            = "advert",
                VehicleModelId   = 1,
                Year             = 100,
                Price            = 100m,
                Power            = 100,
                DistanceCoverage = 100,
                CityId           = 1,
                Description      = "ani description",
                CreatedOn        = DateTime.Now
            };

            var advertService = new Mock <IAdvertService>();

            advertService.Setup(x => x.GetById(It.IsAny <int>())).Returns(new Advert());

            var ajaxAdvertController = new CarAdverts.Web.Areas.Administrator.Controllers.AjaxAdvertController(advertService.Object);

            // Act and Arrange
            ajaxAdvertController
            .WithCallTo(x => x.Update(advertViewModel))
            .ShouldReturnJson(data =>
            {
                Assert.AreEqual(data.Title, advertViewModel.Title);
                Assert.AreEqual(data.VehicleModelId, advertViewModel.VehicleModelId);
                Assert.AreEqual(data.Year, advertViewModel.Year);
                Assert.AreEqual(data.Price, advertViewModel.Price);
                Assert.AreEqual(data.Power, advertViewModel.Power);
                Assert.AreEqual(data.DistanceCoverage, advertViewModel.DistanceCoverage);
                Assert.AreEqual(data.CityId, advertViewModel.CityId);
                Assert.AreEqual(data.Description, advertViewModel.Description);
                Assert.AreEqual(data.CreatedOn, advertViewModel.CreatedOn);
            });
        }
        public void InvokeAdvertServiceMethod_CreateAdvert_Once_WhennModelStateIsValid()
        {
            // Arrange
            // This code mock User.Identity.GetUserId()
            var context  = new Mock <HttpContextBase>();
            var identity = new GenericIdentity("*****@*****.**");

            identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", "1"));
            var principal = new GenericPrincipal(identity, new[] { "user" });

            context.Setup(s => s.User).Returns(principal);
            //

            var advertViewModel = new AdvertViewModel()
            {
                Title            = "advert",
                VehicleModelId   = 1,
                UserId           = "user id",
                Year             = 100,
                Price            = 100m,
                Power            = 100,
                DistanceCoverage = 100,
                CityId           = 1,
                Description      = "ani description",
                CreatedOn        = DateTime.Now
            };

            var advertService        = new Mock <IAdvertService>();
            var ajaxAdvertController = new CarAdverts.Web.Areas.Administrator.Controllers.AjaxAdvertController(advertService.Object);

            ajaxAdvertController.ControllerContext = new ControllerContext(context.Object, new RouteData(), ajaxAdvertController);

            // Act
            ajaxAdvertController.Add(advertViewModel);

            // Arrange
            advertService.Verify(a => a.CreateAdvert(It.IsAny <Advert>(), It.IsAny <IEnumerable <HttpPostedFileBase> >()), Times.Once);
        }
        public void ReturnJsonResultWithCorrectModel()
        {
            // Arrange
            var expectedAdvert = new Advert()
            {
                Id = 1, Title = "title", Description = "test"
            };

            var advertService = new Mock <IAdvertService>();

            advertService.Setup(x => x.GetById(It.IsAny <int>())).Returns(expectedAdvert);

            var ajaxAdvertController = new CarAdverts.Web.Areas.Administrator.Controllers.AjaxAdvertController(advertService.Object);

            // Act
            ajaxAdvertController
            .WithCallTo(x => x.Delete(1))
            .ShouldReturnJson(data =>
            {
                Assert.AreEqual(data.Id, expectedAdvert.Id);
                Assert.AreEqual(data.Title, expectedAdvert.Title);
                Assert.AreEqual(data.Description, expectedAdvert.Description);
            });
        }