public async Task UpdateAlbum() { // Arrange int albumId = new Random().Next(1, 100); string seed = Guid.NewGuid().ToString(); string expectedUserName = string.Format("test_{0}@gmail.com", Guid.NewGuid()); ControllerContext controllerContext = Utilities.SetupCurrentUserForController(expectedUserName); var mockAlbumRepository = new Mock <IAlbumRepository>(); var mockUserRepository = new Mock <IUserRepository>(); User expectedUser = new User() { Id = seed, UserName = expectedUserName }; mockUserRepository.Setup(m => m.GetOrAdd(It.IsAny <User>())).Returns(expectedUser); var mockUnitOfWork = new Mock <IUnitOfWork>(); AlbumsController controller = new AlbumsController(this._mapper, mockAlbumRepository.Object, mockUserRepository.Object, mockUnitOfWork.Object); controller.ControllerContext = controllerContext; var seedAlbum = new Album() { Id = albumId, Name = "Album " + albumId, Author = new User() { UserName = expectedUserName }, }; mockAlbumRepository.Setup(m => m.GetAsync(albumId, true)).ReturnsAsync(seedAlbum); AlbumResource updateResource = new AlbumResource() { Id = albumId, Name = seed }; // Act var result = await controller.UpdateAlbum(albumId, updateResource); // Assert Assert.IsType <OkObjectResult>(result); Assert.IsType <AlbumResource>(((OkObjectResult)result).Value); AlbumResource returnedResource = (AlbumResource)((OkObjectResult)result).Value; Assert.True(returnedResource.Equals(updateResource)); }
public async Task GetPhoto() { // Arrange var seedIds = new List <int> { new Random().Next(1, 50), new Random().Next(51, 100), new Random().Next(101, 150), new Random().Next(151, 200) }; var seedAlbums = SeedAlbums(seedIds); string seed = Guid.NewGuid().ToString(); string expectedUserName = string.Format("test_{0}@gmail.com", seed); ControllerContext controllerContext = Utilities.SetupCurrentUserForController(expectedUserName); var mockAlbumRepository = new Mock <IAlbumRepository>(); var mockUserRepository = new Mock <IUserRepository>(); User expectedUser = new User() { Id = seed, UserName = expectedUserName }; mockUserRepository.Setup(m => m.GetOrAdd(It.IsAny <User>())).Returns(expectedUser); var mockUnitOfWork = new Mock <IUnitOfWork>(); AlbumsController controller = new AlbumsController(this._mapper, mockAlbumRepository.Object, mockUserRepository.Object, mockUnitOfWork.Object); controller.ControllerContext = controllerContext; foreach (var seedAlbum in seedAlbums) { var id = seedAlbum.Id; var seedAlbumResource = this._mapper.Map <Album, AlbumResource>(seedAlbum); mockAlbumRepository.Setup(m => m.GetAsync(id, true)).ReturnsAsync(seedAlbum); // Act var result = await controller.GetAlbum(id); // Assert Assert.IsType <OkObjectResult>(result); Assert.IsType <AlbumResource>(((OkObjectResult)result).Value); AlbumResource returnedAlbumResource = (AlbumResource)((OkObjectResult)result).Value; Assert.True(returnedAlbumResource.Equals(seedAlbumResource)); } }