示例#1
0
        public async Task GetAuthorByIdShouldreturnAuthorWithSelectedId()
        {
            var optionsBuilder = new DbContextOptionsBuilder <BookTubeContext>()
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var dbContext = new BookTubeContext(optionsBuilder.Options);
            var service   = new AuthorsService(dbContext);

            var authors = new List <Author>
            {
                new Author {
                    Id = 1, Name = "Ivan Vazov", Bio = "Born in Sopot, Bulgaria"
                },
                new Author {
                    Id = 2, Name = "Aleko Konstantinov", Bio = "Born in Svishtov, Bulgaria"
                },
                new Author {
                    Id = 3, Name = "Elin Pelin", Bio = "Born in Bailovo, Bulgaria"
                }
            };

            foreach (var author in authors)
            {
                await dbContext.Authors.AddAsync(author);
            }
            await dbContext.SaveChangesAsync();

            var expectedAuthor = authors.First(x => x.Id == 2);
            var actual         = service.GetAuthorById(2);

            Assert.Equal(expectedAuthor, actual);
        }
        public IActionResult GetAuthorById(int authorId)
        {
            var author = _authorsService.GetAuthorById(authorId);

            return(Ok(author));
        }