public async Task Test_Create_Author_Async() { _mockSet.As <IDbAsyncEnumerable <AuthorDb> >().Setup(b => b.GetAsyncEnumerator()).Returns(new TestDbAsyncEnumerator <AuthorDb>(_autorsDb.GetEnumerator())); _mockSet.As <IQueryable <AuthorDb> >().Setup(b => b.Provider).Returns(new TestDbAsyncQueryProvider <AuthorDb>(_autorsDb.Provider)); _mockContext.Setup(c => c.Authors).Returns(_mockSet.Object); _mockRepo = new AuthorRepository(_mockContext.Object); _mockMapper.Setup(m => m.Map <AuthorInfo, AuthorDb>(_oneAuthorInfo)).Returns(_oneAuthorDb); _service = new AuthorService(_mockMapper.Object, _mockRepo); var result = await _service.AddAsync(_oneAuthorInfo); _mockSet.Verify(b => b.Add(It.IsAny <AuthorDb>()), Times.Once()); _mockContext.Verify(b => b.SaveChangesAsync(), Times.Once()); result.IsSuccess.Should().BeTrue(); }
public async Task CreateAuthorShouldThrowExeption() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var repository = new EfDeletableEntityRepository <Autor>(dbContext); var service = new AuthorService(repository); string firstName = null; var lastName = "LastName"; await Assert.ThrowsAsync <ArgumentException>(async() => { await service.AddAsync(firstName, lastName); }); }
public async Task CreateAuthorShouldReturnCorrectValue() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var repository = new EfDeletableEntityRepository <Autor>(dbContext); var service = new AuthorService(repository); var firstName = "Author1"; var lastName = "Author1"; await service.AddAsync(firstName, lastName); var actualResult = repository.All().FirstOrDefault(); Assert.Equal(firstName, actualResult.FirstName); Assert.Equal(lastName, actualResult.LastName); }
public async Task <Author> Add([FromBody] Author author) { return(await _authorService.AddAsync(author)); }