Пример #1
0
 public async Task <IEnumerable <Album> > Get([FromQuery] int?userId)
 {
     if (userId.HasValue)
     {
         return(await integratorService.GetByUserIdAsync(userId.Value));
     }
     else
     {
         return(await integratorService.GetAllAsync());
     }
 }
Пример #2
0
        public void Correctly_Filters_By_UserId()
        {
            // Arrange
            var userId        = 3;
            var mockAlbumRepo = mocker.GetMock <IAlbumRepository>();
            var mockPhotoRepo = mocker.GetMock <IPhotoRepository>();

            mockAlbumRepo.Setup(x => x.GetAsync(userId)).ReturnsAsync(new[]
            {
                new Album {
                    Id = 4, UserId = 3
                },
                new Album {
                    Id = 5, UserId = 3
                }
            })
            .Verifiable();

            mockPhotoRepo.Setup(x => x.GetAsync(4)).ReturnsAsync(new[]
            {
                new Photo {
                    Id = 7, AlbumId = 4
                },
                new Photo {
                    Id = 8, AlbumId = 4
                }
            })
            .Verifiable();

            mockPhotoRepo.Setup(x => x.GetAsync(5)).ReturnsAsync(new[]
            {
                new Photo {
                    Id = 9, AlbumId = 5
                }
            })
            .Verifiable();

            mocker.Use(mockAlbumRepo.Object);
            mocker.Use(mockPhotoRepo.Object);

            // Act
            var actual = SUT.GetByUserIdAsync(userId).Result;

            // Assert
            mockAlbumRepo.VerifyAll();
            mockPhotoRepo.VerifyAll();

            actual.Should().BeEquivalentTo(new[]
            {
                new
                {
                    Id     = 4, UserId = 3,
                    Photos = new[]
                    {
                        new { Id = 7, AlbumId = 4 },
                        new { Id = 8, AlbumId = 4 }
                    }
                },
                new
                {
                    Id     = 5, UserId = 3,
                    Photos = new[]
                    {
                        new { Id = 9, AlbumId = 5 }
                    }
                }
            });
        }