public void ReturnCorrectResult_IfCollectionIsNotEmpty() { // Arrange var mockedCollection = Utils.GetLocationsCollection(); var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.Locations).Returns(mockedDbSet.Object); var locationService = new LocationService(mockedDbContext.Object); // Act var result = locationService.GetAll(); // Assert Assert.IsTrue(result.Count() == 3); var index = 0; foreach (var location in result) { Assert.AreEqual(mockedCollection[index].Name, location.Name); index++; } }
public void CreateNewGallery_IfNotExist_AndAddVideoToGallery() { // Arrange var video = new Video() { Title = "Test video" }; var mockedGallery = Utils.GetEmptyVideoGallery(); var mockedDbSet = MockDbSet.Mock(mockedGallery.AsQueryable()); mockedDbSet.Setup(d => d.Add(It.IsAny <VideoGallery>())).Callback <VideoGallery>((g) => mockedGallery.Add(g)); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.VideoGalleries).Returns(mockedDbSet.Object); var videoService = new VideoService(mockedDbContext.Object); var newGalleryName = "Test"; // Act videoService.AddVideoToGallery(newGalleryName, video); // Assert Assert.IsTrue(mockedDbContext.Object.VideoGalleries.Count() == 1); Assert.IsTrue(mockedDbContext.Object.VideoGalleries.First().Name == newGalleryName); Assert.IsTrue(mockedDbContext.Object.VideoGalleries.First().Videos.Count == 1); Assert.AreEqual(mockedDbContext.Object.VideoGalleries.First().Videos.First(), video); }
public void AddImageToGallery_IfIdMatch() { // Arrange var image = new Image(); var gallery = new ImageGallery() { Name = "Test gallery" }; var mockedCollection = new List <ImageGallery>() { gallery }; var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); mockedDbSet.Setup(d => d.Find(It.IsAny <object[]>())).Returns <object[]>(ids => mockedCollection.FirstOrDefault(d => d.Id == ids[0].ToString())); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.ImageGalleries).Returns(mockedDbSet.Object); var imageGalleryService = new ImageGalleryService(mockedDbContext.Object); // Act imageGalleryService.AddImageToGallery(image, gallery.Id); // Assert Assert.IsTrue(gallery.Images.Count == 1); Assert.IsTrue(gallery.Images.Contains(image)); }
public void AddImageGalleryToDbContext() { // Arrange var gallery = new ImageGallery() { Name = "Test gallery" }; var mockedCollection = new List <ImageGallery>(); var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); mockedDbSet.Setup(d => d.Add(It.IsAny <ImageGallery>())).Callback <ImageGallery>((g) => mockedCollection.Add(g)); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.ImageGalleries).Returns(mockedDbSet.Object); var imageGallertService = new ImageGalleryService(mockedDbContext.Object); // Act imageGallertService.Add(gallery); // Assert Assert.IsTrue(mockedCollection.Count == 1); Assert.AreEqual(gallery, mockedCollection[0]); }
public void ReturnCorrectResult_IfCollectionIsNotEmpty() { // Arrange var fishCollection = Utils.GetFishCollection(); var mockedDbSet = MockDbSet.Mock(fishCollection.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.Fish).Returns(mockedDbSet.Object); var fishService = new FishService(mockedDbContext.Object); // Act var allFish = fishService.GetAll(); // Assert Assert.IsTrue(allFish.Count() == fishCollection.Count); var index = 0; foreach (var fish in allFish) { Assert.AreEqual(fishCollection[index].Name, fish.Name); Assert.AreEqual(fishCollection[index].ImageUrl, fish.ImageUrl); index++; } }
public void AddImageToGallery_IfNameMatch() { // Arrange var image = new Image(); var gallery = new ImageGallery() { Name = "Test gallery" }; var mockedCollection = new List <ImageGallery>() { gallery }; var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.ImageGalleries).Returns(mockedDbSet.Object); var imageGalleryService = new ImageGalleryService(mockedDbContext.Object); // Act imageGalleryService.AddImageToGallery(gallery.Name, image); // Assert Assert.IsTrue(gallery.Images.Count == 1); Assert.IsTrue(gallery.Images.Contains(image)); }
public void AddLakeToDbContext() { // Arrange var lake = new Lake() { Name = "Test lake" }; var mockedCollection = new List <Lake>(); var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); mockedDbSet.Setup(d => d.Add(It.IsAny <Lake>())).Callback <Lake>((l) => mockedCollection.Add(l)); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.Lakes).Returns(mockedDbSet.Object); var lakeService = new LakeService(mockedDbContext.Object); // Act lakeService.Add(lake); // Assert Assert.IsTrue(mockedCollection.Count == 1); Assert.AreEqual(lake, mockedCollection[0]); }
public void ReturnCorrectResult_IfIdMatch() { // Arrange var gallery = new ImageGallery() { Name = "Test gallery" }; var mockedCollection = new List <ImageGallery>() { new ImageGallery(), gallery, new ImageGallery() }; var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); mockedDbSet.Setup(d => d.Find(It.IsAny <object[]>())).Returns <object[]>(ids => mockedCollection.FirstOrDefault(d => d.Id == ids[0].ToString())); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.ImageGalleries).Returns(mockedDbSet.Object); var imageGalleryService = new ImageGalleryService(mockedDbContext.Object); // Act var result = imageGalleryService.FindById(gallery.Id); // Assert Assert.AreEqual(gallery, result); }
public void ReturnTrue_IfGalleryExist_AndVideoIsRemoved() { // Arrange var mockedGalleries = Utils.GetVideoGalleriesCollection(); var video = new Video(); var gallery = mockedGalleries.First(); gallery.Videos.Add(video); var mockedDbSet = MockDbSet.Mock(mockedGalleries.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.VideoGalleries).Returns(mockedDbSet.Object); var videoService = new VideoService(mockedDbContext.Object); // Confirm that the gallery contains the video before remove it Assert.IsTrue(gallery.Videos.Count == 1); Assert.AreEqual(gallery.Videos.First(), video); // Act var isRemoved = videoService.RemoveVideoFromGallery(gallery.Name, video.Id); Assert.IsTrue(gallery.Videos.Count == 0); Assert.IsTrue(isRemoved); }
public void ReturnNull_IfIdNotMatch() { // Arrange var mockedGalleriesCollection = Utils.GetVideoGalleriesCollection(); // Add videos for every gallery, so there won't be empty gallery in the context. foreach (var gallery in mockedGalleriesCollection) { gallery.Videos.Add(new Video() { Title = "First video" }); gallery.Videos.Add(new Video() { Title = "Second video" }); } var searchedGalleryId = Guid.NewGuid().ToString(); var mockedDbSet = MockDbSet.Mock(mockedGalleriesCollection.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.VideoGalleries).Returns(mockedDbSet.Object); var videoService = new VideoService(mockedDbContext.Object); // Act var videos = videoService.GetVideosFromGallery(searchedGalleryId); // Assert Assert.IsNull(videos); }
public void ReturnCorrectResult() { // Arrange var mockedCollection = Utils.GetLakesCollection(); var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.Lakes).Returns(mockedDbSet.Object); var lakeService = new LakeService(mockedDbContext.Object); // Act var result = lakeService.GetAll(); // Assert Assert.IsTrue(result.Count() == mockedCollection.Count); var index = 0; foreach (var lake in result) { Assert.AreEqual(mockedCollection[index].Name, lake.Name); Assert.AreEqual(mockedCollection[index].Id, lake.Id); index++; } }
public void ReturnCorectResult_IfIdMatch() { // Arrange var mockedGalleriesCollection = Utils.GetVideoGalleriesCollection(); var searchedGallery = mockedGalleriesCollection[1]; var searchedGalleryId = searchedGallery.Id; searchedGallery.Videos.Add(new Video() { Title = "First video" }); searchedGallery.Videos.Add(new Video() { Title = "Second video" }); var mockedDbSet = MockDbSet.Mock(mockedGalleriesCollection.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.VideoGalleries).Returns(mockedDbSet.Object); var videoService = new VideoService(mockedDbContext.Object); // Act var videos = videoService.GetVideosFromGallery(searchedGalleryId); // Assert Assert.IsTrue(videos.Count() == 2); Assert.IsTrue(videos.First().Title == "First video"); Assert.IsTrue(videos.Last().Title == "Second video"); }
public void SetImageIsConfirmed_ToTrue_IfIdMatch() { // Arrange var mockedCollection = new List <Image>() { new Image() { IsConfirmed = true }, new Image() { IsConfirmed = false }, new Image() { IsConfirmed = true }, }; var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); mockedDbSet.Setup(d => d.Find(It.IsAny <object[]>())).Returns <object[]>(ids => mockedCollection.FirstOrDefault(d => d.Id == ids[0].ToString())); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.Images).Returns(mockedDbSet.Object); var service = new ImageGalleryService(mockedDbContext.Object); var searchedImageId = mockedCollection[1].Id; // Act service.ConfirmImage(searchedImageId); // Assert Assert.IsTrue(mockedCollection[1].IsConfirmed); }
public void ReturnOnlyGalleries_ThatHaveUnconfirmedImages() { // Arrange var mockedCollection = this.GetGalleries(); var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.ImageGalleries).Returns(mockedDbSet.Object); var service = new ImageGalleryService(mockedDbContext.Object); // Act var result = service.GetGalleriesWithUnconfirmedImages(); // Assert Assert.IsTrue(result.Count() == 1); }
public void ReturnAllUnconfirmedImages() { // Arrange var mockedImageCollection = new List <Image>() { new Image() { IsConfirmed = false }, new Image() { IsConfirmed = true }, new Image() { IsConfirmed = false } }; var mockedGalleryCollection = new List <ImageGallery>() { new ImageGallery(), new ImageGallery() { Images = mockedImageCollection }, new ImageGallery() }; var mockedDbSet = MockDbSet.Mock(mockedGalleryCollection.AsQueryable()); mockedDbSet.Setup(d => d.Find(It.IsAny <object[]>())).Returns <object[]>(ids => mockedGalleryCollection.FirstOrDefault(d => d.Id == ids[0].ToString())); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.ImageGalleries).Returns(mockedDbSet.Object); var imageGalleryService = new ImageGalleryService(mockedDbContext.Object); var searchedGalleryId = mockedGalleryCollection[1].Id; // Act var result = imageGalleryService.GetAllUnconfirmed(searchedGalleryId); // Assert Assert.IsTrue(result.Count() == 2); }
public void ReturnCorrectResult() { // Arrange var mockedCollection = Utils.GetNewsCollection(); var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.News).Returns(mockedDbSet.Object); var newsService = new NewsService(mockedDbContext.Object); // Act var result = newsService.GetNewsCount(); // Assert Assert.AreEqual(mockedCollection.Count, result); }
public void ReturnCorrectResult_IfCollectionIsEmpty() { // Arrange var mockedCollection = new List <Location>(); var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.Locations).Returns(mockedDbSet.Object); var locationService = new LocationService(mockedDbContext.Object); // Act var result = locationService.GetAll(); // Assert Assert.IsTrue(result.Count() == 0); }
public void ReturnCorrectResult_IfLocationNotMatch() { // Arrange var mockedCollection = Utils.GetLakesCollection(); var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.Lakes).Returns(mockedDbSet.Object); var lakeService = new LakeService(mockedDbContext.Object); // Act var result = lakeService.FindByLocation("Not Valid"); // Assert Assert.IsTrue(result.Count() == 0); }
public void ReturnCorrectResult_IfParameterIsOutOfRabge() { // Arrange var mockedCollection = Utils.GetNewsCollection(); var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.News).Returns(mockedDbSet.Object); var newsService = new NewsService(mockedDbContext.Object); // Act var result = newsService.GetNews(3, 1); // Assert Assert.IsTrue(result.Count() == 0); }
public void ReturnNull_IfNameNotMatched() { // Arrange var fishCollection = Utils.GetFishCollection(); var mockedDbSet = MockDbSet.Mock(fishCollection.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.Fish).Returns(mockedDbSet.Object); var fishService = new FishService(mockedDbContext.Object); // Act var fish = fishService.GetFishByName("invalid name"); // Assert Assert.IsNull(fish); }
public void ReturnCorrectResult_IfUsernameNotMatched() { // Arrange var mockedCollection = Utils.GetCommentsCollection(); var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.Comments).Returns(mockedDbSet.Object); var commentService = new CommentService(mockedDbContext.Object); // Act var result = commentService.GetAllByUsername("invalid name"); // Assert Assert.IsTrue(result.Count() == 0); }
public void ReturnCorrectResult_IfIdNotMatch() { // Arrange var mockedColletion = Utils.GetVideoCollection(); var mockedDbSet = MockDbSet.Mock(mockedColletion.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.Videos).Returns(mockedDbSet.Object); var videoService = new VideoService(mockedDbContext.Object); // Act var result = videoService.GetVideoById("some id"); // Assert Assert.IsNull(result); }
public void ReturnEmptyCollection_IfCollectionContainsFish_AndTypeNotMatch() { // Arrange var allFish = this.GetFish(); var mockedDbSet = MockDbSet.Mock(allFish.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.Fish).Returns(mockedDbSet.Object); var fishService = new FishService(mockedDbContext.Object); // Act var fishByType = fishService.GetAllByType(FishType.Saltwater); // Assert Assert.IsTrue(fishByType.Count() == 0); }
public void ReturnCorrectResult_IfNameNotMatch() { // Arrange var mockedCollection = Utils.GetLakesCollection(); var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.Lakes).Returns(mockedDbSet.Object); var lakeService = new LakeService(mockedDbContext.Object); // Act var result = lakeService.FindByName("Invalid name"); // Assert Assert.IsNull(result); }
public void ReturnAllImagesFromGallery_ThatAreConfirmed() { // Arrange var gallery = new ImageGallery() { Name = "Test gallery" }; gallery.Images.Add(new Image() { IsConfirmed = false }); gallery.Images.Add(new Image() { IsConfirmed = true }); gallery.Images.Add(new Image() { IsConfirmed = true }); var mockedCollection = new List <ImageGallery>() { new ImageGallery(), gallery, new ImageGallery() }; var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); mockedDbSet.Setup(d => d.Find(It.IsAny <object[]>())).Returns <object[]>(ids => mockedCollection.FirstOrDefault(d => d.Id == ids[0].ToString())); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.ImageGalleries).Returns(mockedDbSet.Object); var imageGalleryService = new ImageGalleryService(mockedDbContext.Object); // Act var result = imageGalleryService.GetAllImages(gallery.Id); // Assert Assert.IsTrue(result.Count() == 2); }
public void ReturnFalse_IfGalleryNotExist() { // Arrange var mockedGalleries = Utils.GetVideoGalleriesCollection(); var video = new Video(); var mockedDbSet = MockDbSet.Mock(mockedGalleries.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.VideoGalleries).Returns(mockedDbSet.Object); var videoService = new VideoService(mockedDbContext.Object); // Act var isRemoved = videoService.RemoveVideoFromGallery("Some name", video.Id); Assert.IsFalse(isRemoved); }
public void ReturnCorrectResult_IfNameMatch() { // Arrange var searchedLakeName = "Test lake"; var mockedCollection = new List <ImageGallery>() { new ImageGallery() { Name = "Test gallery", Lake = new Lake() { Name = searchedLakeName } }, new ImageGallery() { Lake = new Lake() { Name = "Test" } }, new ImageGallery() { Name = "Test gallery 2", Lake = new Lake() { Name = searchedLakeName } } }; var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.ImageGalleries).Returns(mockedDbSet.Object); var imageGalleryService = new ImageGalleryService(mockedDbContext.Object); // Act var result = imageGalleryService.GetByLake(searchedLakeName); // Assert Assert.IsTrue(result.Count() == 2); }
public void ReturnNull_IfNameNotMatch() { // Arrange var mockedCollection = Utils.GetLocationsCollection(); var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.Locations).Returns(mockedDbSet.Object); var locationService = new LocationService(mockedDbContext.Object); var expectedLocation = mockedCollection[1]; // Act var result = locationService.FindByName("Invalid name"); // Assert Assert.IsNull(result); }
public void ReturnCorrectResult_IfNameMatch() { // Arrange var mockedCollection = Utils.GetLakesCollection(); var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.Lakes).Returns(mockedDbSet.Object); var lakeService = new LakeService(mockedDbContext.Object); var searchedLake = mockedCollection[1]; // Act var result = lakeService.FindByName(searchedLake.Name); // Assert Assert.AreEqual(searchedLake, result); }
public void ReturnCorrectResult_IfIdNotMatch() { // Arrange var mockedCollection = Utils.GetNewsCollection(); var mockedDbSet = MockDbSet.Mock(mockedCollection.AsQueryable()); mockedDbSet.Setup(d => d.Find(It.IsAny <object[]>())).Returns <object[]>(ids => mockedCollection.FirstOrDefault(d => d.Id == ids[0].ToString())); var mockedDbContext = new Mock <IDatabaseContext>(); mockedDbContext.Setup(c => c.News).Returns(mockedDbSet.Object); var newsService = new NewsService(mockedDbContext.Object); // Act var result = newsService.FindById("some id"); // Assert Assert.IsNull(result); }