public void AddErrorToModelState_IfFileIsNull() { // Arrange var mockedImageGalleryService = new Mock <IImageGalleryService>(); var mockedImageFactory = new Mock <IImageFactory>(); var mockedDateProvider = new Mock <IDateProvider>(); var mockedLakeService = new Mock <ILakeService>(); mockedLakeService.Setup(s => s.GetAll()).Verifiable(); var mockedImageGalleryFactory = new Mock <IImageGalleryFactory>(); var mockedDirectoryHelper = new Mock <IDirectoryHelper>(); var controller = new PicturesController( mockedImageGalleryService.Object, mockedImageFactory.Object, mockedDateProvider.Object, mockedLakeService.Object, mockedImageGalleryFactory.Object, mockedDirectoryHelper.Object); var model = new AddImageViewModel(); // Act var result = controller.Add(null, model) as ViewResult; // Assert Assert.AreEqual( GlobalMessages.NoFileErrorMessage, result.ViewData.ModelState[""].Errors.First().ErrorMessage); }
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 GetLakesFromService_AndRetunDefaultView() { // Arrange var mockedLakesCollection = new List <LakeModel>() { new LakeModel() { Name = "Test" } }; var mockedImageGalleryService = new Mock <IImageGalleryService>(); var mockedImageFactory = new Mock <IImageFactory>(); var mockedDateProvider = new Mock <IDateProvider>(); var mockedLakeService = new Mock <ILakeService>(); mockedLakeService.Setup(s => s.GetAll()).Returns(mockedLakesCollection).Verifiable(); var mockedImageGalleryFactory = new Mock <IImageGalleryFactory>(); var mockedDirectoryHelper = new Mock <IDirectoryHelper>(); var controller = new PicturesController( mockedImageGalleryService.Object, mockedImageFactory.Object, mockedDateProvider.Object, mockedLakeService.Object, mockedImageGalleryFactory.Object, mockedDirectoryHelper.Object); // Act var result = controller.Add() as ViewResult; var model = result.Model as AddImageViewModel; // Assert Assert.AreEqual("", result.ViewName); Assert.AreEqual(mockedLakesCollection, model.Lakes); mockedLakeService.Verify(s => s.GetAll(), 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); }