public MockHttpFilesCollection(MockHttpPostedFileBase file)
 {
     if (file != null)
     {
         _files.Add(file.FileName, file);
     }
 }
Esempio n. 2
0
        public void ReturnViewWithModelErrors_IfAddindNewsFailed()
        {
            // Arrange
            var mockedNewsFactory = new Mock <INewsFactory>();

            mockedNewsFactory.Setup(f => f.CreateNews(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <DateTime>())).Verifiable();

            var mockedNewsService = new Mock <INewsService>();

            mockedNewsService.Setup(s => s.Add(It.IsAny <News>())).Verifiable();
            mockedNewsService.Setup(s => s.Save()).Throws(new Exception("Test"));

            var mockedDateProvider = new Mock <IDateProvider>();

            mockedDateProvider.Setup(d => d.GetDate()).Verifiable();

            var mockedHttpContext = new Mock <ControllerContext>();

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

            var controller = new NewsController(mockedNewsFactory.Object, mockedNewsService.Object, mockedDateProvider.Object);

            controller.ControllerContext = mockedHttpContext.Object;

            var model      = new AddNewsViewModel();
            var mockedFile = new MockHttpPostedFileBase();

            mockedFile.SetContentLength(Constants.ImageMaxSize);

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

            // Assert
            ModelState modelError;

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

            Assert.AreEqual("", result.ViewName);
            Assert.IsTrue(modelError.Errors.First().ErrorMessage == "Test");
            Assert.IsTrue(result.TempData[GlobalMessages.AddNewsSuccessKey] == null);

            mockedNewsFactory.Verify(f => f.CreateNews(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <DateTime>()), Times.Once);

            mockedNewsService.Verify(s => s.Add(It.IsAny <News>()), Times.Once);

            mockedDateProvider.Verify(d => d.GetDate(), Times.Once);
        }
Esempio n. 3
0
        public void ReturnViewWithModelErrors_IfModelStateIsNotValid()
        {
            // Arrange
            var mockedNewsFactory = new Mock <INewsFactory>();

            mockedNewsFactory.Setup(f => f.CreateNews(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <DateTime>())).Verifiable();

            var mockedNewsService = new Mock <INewsService>();

            mockedNewsService.Setup(s => s.Add(It.IsAny <News>())).Verifiable();
            mockedNewsService.Setup(s => s.Save()).Verifiable();

            var mockedDateProvider = new Mock <IDateProvider>();

            mockedDateProvider.Setup(d => d.GetDate()).Verifiable();

            var controller = new NewsController(mockedNewsFactory.Object, mockedNewsService.Object, mockedDateProvider.Object);

            var expextedError        = "test error";
            var expectedErrorMessage = "Test";

            controller.ModelState.AddModelError(expextedError, expectedErrorMessage);

            var model      = new AddNewsViewModel();
            var mockedFile = new MockHttpPostedFileBase();

            mockedFile.SetContentLength(Constants.ImageMaxSize);

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

            // Assert
            ModelState modelError;

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

            Assert.AreEqual("", result.ViewName);
            Assert.IsTrue(modelError.Errors.First().ErrorMessage == expectedErrorMessage);
            Assert.IsTrue(result.TempData[GlobalMessages.AddNewsSuccessKey] == null);

            mockedNewsFactory.Verify(f => f.CreateNews(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <DateTime>()), Times.Never);

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

            mockedDateProvider.Verify(d => d.GetDate(), Times.Never);
        }
        public void AddErrorMessageToModel_IfAddingImageFail()
        {
            // Arrange
            var mockedImageGalleryService = new Mock <IImageGalleryService>();
            var mockedImageFactory        = new Mock <IImageFactory>();
            var mockedDateProvider        = new Mock <IDateProvider>();

            mockedDateProvider.Setup(d => d.GetDate()).Throws(new ArgumentException(message: "Test"));

            var mockedLakeService = new Mock <ILakeService>();

            mockedLakeService.Setup(s => s.GetAll()).Verifiable();

            var mockedImageGalleryFactory = new Mock <IImageGalleryFactory>();
            var mockedDirectoryHelper     = new Mock <IDirectoryHelper>();

            var mockedHttpContext = new Mock <ControllerContext>();

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

            var controller = new PicturesController(
                mockedImageGalleryService.Object,
                mockedImageFactory.Object,
                mockedDateProvider.Object,
                mockedLakeService.Object,
                mockedImageGalleryFactory.Object,
                mockedDirectoryHelper.Object);

            controller.ControllerContext = mockedHttpContext.Object;

            var model      = new AddImageViewModel();
            var mockedFile = new MockHttpPostedFileBase();

            mockedFile.SetContentLength(Constants.ImageMaxSize);

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

            // Assert
            Assert.AreEqual(
                "Test",
                result.ViewData.ModelState[""].Errors.First().ErrorMessage);
        }
        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);
        }
        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);
        }
        public void LeaveImageAsUnconfirmed_IfUserIsNotInRoleAsModerator()
        {
            // Arrange
            var mockedGallery = new ImageGallery();
            var mockedImage   = new Image();

            var mockedImageGalleryService = new Mock <IImageGalleryService>();

            mockedImageGalleryService.Setup(g => g.FindById(It.IsAny <string>()))
            .Returns(mockedGallery)
            .Verifiable();
            mockedImageGalleryService.Setup(s => s.Save()).Verifiable();

            var mockedImageFactory = new Mock <IImageFactory>();

            mockedImageFactory.Setup(f => f.CreateImage(It.IsAny <string>(), It.IsAny <DateTime>(), It.IsAny <string>()))
            .Returns(mockedImage)
            .Verifiable();

            var mockedDateProvider = new Mock <IDateProvider>();

            mockedDateProvider.Setup(d => d.GetDate()).Verifiable();

            var mockedLakeService = new Mock <ILakeService>();

            mockedLakeService.Setup(s => s.GetAll()).Verifiable();

            var mockedImageGalleryFactory = new Mock <IImageGalleryFactory>();
            var mockedDirectoryHelper     = new Mock <IDirectoryHelper>();

            mockedDirectoryHelper.Setup(d => d.CreateIfNotExist(It.IsAny <string>())).Verifiable();

            var mockedHttpContext = new Mock <ControllerContext>();

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

            var controller = new PicturesController(
                mockedImageGalleryService.Object,
                mockedImageFactory.Object,
                mockedDateProvider.Object,
                mockedLakeService.Object,
                mockedImageGalleryFactory.Object,
                mockedDirectoryHelper.Object);

            controller.ControllerContext = mockedHttpContext.Object;

            var model = new AddImageViewModel();

            model.SelectedImageGalleryId = "testId";
            var mockedFile = new MockHttpPostedFileBase();

            mockedFile.SetContentLength(Constants.ImageMaxSize);

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

            // Assert
            Assert.IsTrue(mockedGallery.Images.Contains(mockedImage));
            Assert.IsTrue(mockedFile.IsSaveAsCalled);
            Assert.IsFalse(mockedImage.IsConfirmed);

            mockedImageGalleryService.Verify(s => s.Save(), Times.Once);
            mockedImageGalleryService.Verify(s => s.FindById("testId"), Times.Once);

            mockedDateProvider.Verify(d => d.GetDate(), Times.Once);

            mockedLakeService.Verify(l => l.GetAll(), Times.Once);

            mockedDirectoryHelper.Verify(d => d.CreateIfNotExist("Test"), Times.Once);
        }