コード例 #1
0
        public void ReturnCorrectErrorMessage_IfFileIsNull()
        {
            // Arrange
            var mockedFishService = new Mock <IFishService>();

            mockedFishService.Setup(s => s.Add(It.IsAny <Fish>())).Verifiable();
            mockedFishService.Setup(s => s.Save()).Verifiable();

            var mockedFishFactory = new Mock <IFishFactory>();

            mockedFishFactory.Setup(f => f.CreateFish(It.IsAny <string>(), It.IsAny <FishType>(), It.IsAny <string>(), It.IsAny <string>())).Verifiable();

            var controller = new FishController(mockedFishFactory.Object, mockedFishService.Object);
            var model      = new AddFishViewModel()
            {
                FishName = "Test", FishType = FishType.SeaFish, Info = "Test"
            };

            // Act
            var result = controller.Add(model, null) as ViewResult;

            // Assert
            ModelState modelError;

            result.ViewData.ModelState.TryGetValue("", out modelError);

            Assert.IsNull(result.TempData[GlobalMessages.FishAddedSuccessKey]);
            Assert.AreEqual(GlobalMessages.FishAddingErrorMessage, modelError.Errors.First().ErrorMessage);

            mockedFishService.Verify(s => s.Add(It.IsAny <Fish>()), Times.Never);
            mockedFishService.Verify(s => s.Save(), Times.Never);

            mockedFishFactory.Verify(f => f.CreateFish(It.IsAny <string>(), It.IsAny <FishType>(), It.IsAny <string>(), It.IsAny <string>()), Times.Never);
        }
コード例 #2
0
        public ActionResult Add(AddFishViewModel model, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (file != null && file.ContentLength <= Constants.ImageMaxSize)
                    {
                        var filePath = Constants.FishImagesFolder + file.FileName;
                        file.SaveAs(HttpContext.Server.MapPath(Constants.FishImagesServerFolder)
                                    + file.FileName);

                        var fish = this.fishFactory.CreateFish(model.FishName, model.FishType, filePath, model.Info);
                        this.fishService.Add(fish);
                        this.fishService.Save();

                        TempData[GlobalMessages.FishAddedSuccessKey] = GlobalMessages.FishAddedSuccessMessage;
                    }
                    else
                    {
                        ModelState.AddModelError("", GlobalMessages.FishAddingErrorMessage);
                    }
                }
                catch (Exception)
                {
                    ModelState.AddModelError("", GlobalMessages.FishAddingFailMessage);
                }

                return(View(model));
            }

            return(View(model));
        }
コード例 #3
0
        public void ReturnCorrectErrorMessage_IfAddingFishFailed()
        {
            // Arrange
            var mockedFishService = new Mock <IFishService>();

            mockedFishService.Setup(s => s.Add(It.IsAny <Fish>())).Verifiable();
            mockedFishService.Setup(s => s.Save()).Throws <Exception>();

            var mockedFishFactory = new Mock <IFishFactory>();

            mockedFishFactory.Setup(f => f.CreateFish(It.IsAny <string>(), It.IsAny <FishType>(), It.IsAny <string>(), It.IsAny <string>())).Verifiable();

            var mockedFile = new MockHttpPostedFileBase();

            mockedFile.SetContentLength(Constants.ImageMaxSize);

            var mockedHttpContext = new Mock <ControllerContext>();

            mockedHttpContext.Setup(c => c.HttpContext.Server.MapPath(It.IsAny <string>())).Returns("Test");

            var controller = new FishController(mockedFishFactory.Object, mockedFishService.Object);

            controller.ControllerContext = mockedHttpContext.Object;
            var model = new AddFishViewModel()
            {
                FishName = "Test", FishType = FishType.SeaFish, Info = "Test"
            };

            // Act
            var result = controller.Add(model, mockedFile) as ViewResult;

            // Assert
            ModelState modelError;

            result.ViewData.ModelState.TryGetValue("", out modelError);

            Assert.IsNull(result.TempData[GlobalMessages.FishAddedSuccessKey]);
            Assert.AreEqual(GlobalMessages.FishAddingFailMessage, modelError.Errors.First().ErrorMessage);

            mockedFishService.Verify(s => s.Add(It.IsAny <Fish>()), Times.Once);

            mockedFishFactory.Verify(f => f.CreateFish(It.IsAny <string>(), It.IsAny <FishType>(), It.IsAny <string>(), It.IsAny <string>()), Times.Once);
        }
コード例 #4
0
        public void ReturnDefaultView_AndSetSuccessMessageToTempData_IfAddingFishNotFailed()
        {
            // Arrange
            var mockedFishService = new Mock <IFishService>();

            mockedFishService.Setup(s => s.Add(It.IsAny <Fish>())).Verifiable();
            mockedFishService.Setup(s => s.Save()).Verifiable();

            var mockedFishFactory = new Mock <IFishFactory>();

            mockedFishFactory.Setup(f => f.CreateFish(It.IsAny <string>(), It.IsAny <FishType>(), It.IsAny <string>(), It.IsAny <string>())).Verifiable();

            var mockedFile = new MockHttpPostedFileBase();

            mockedFile.SetContentLength(Constants.ImageMaxSize);

            var mockedHttpContext = new Mock <ControllerContext>();

            mockedHttpContext.Setup(c => c.HttpContext.Server.MapPath(It.IsAny <string>())).Returns("Test");

            var controller = new FishController(mockedFishFactory.Object, mockedFishService.Object);

            controller.ControllerContext = mockedHttpContext.Object;
            var model = new AddFishViewModel()
            {
                FishName = "Test", FishType = FishType.SeaFish, Info = "Test"
            };

            // Act
            var result = controller.Add(model, mockedFile) as ViewResult;

            // Assert
            Assert.IsNotNull(result.TempData[GlobalMessages.FishAddedSuccessKey]);
            Assert.AreEqual("", result.ViewName);
            Assert.AreEqual(model, result.ViewData.Model);

            mockedFishService.Verify(s => s.Add(It.IsAny <Fish>()), Times.Once);
            mockedFishService.Verify(s => s.Save(), Times.Once);

            mockedFishFactory.Verify(f => f.CreateFish(It.IsAny <string>(), It.IsAny <FishType>(), It.IsAny <string>(), It.IsAny <string>()), Times.Once);
        }
コード例 #5
0
        public ActionResult Add()
        {
            var model = new AddFishViewModel();

            return(View(model));
        }