public async Task Post() { await DbContext.Tags.AddAsync(new Tag { Name = ".net" }); await DbContext.SaveChangesAsync(); var svc = new ArticleService(DbContext, Mapper); var postDto = new PostArticleDto { Title = Guid.NewGuid().ToString(), CategoryId = ArticleCategory.Database, Content = Guid.NewGuid().ToString(), Status = BaseStatus.Disabled, TagIds = new List <int> { 1, 2, 1 } }; await svc.PostAsync(postDto); var articles = await DbContext.Articles .AsNoTracking() .Include(a => a.ArticleTags) .ThenInclude(at => at.Tag) .ToListAsync(); var totalTags = await DbContext.Tags.CountAsync(); Assert.AreEqual(1, articles.Count); Assert.AreEqual(1, articles[0].Id); Assert.AreEqual(postDto.Title, articles[0].Title); Assert.AreEqual(postDto.CategoryId, articles[0].CategoryId); Assert.AreEqual(postDto.Content, articles[0].Content); Assert.AreEqual(postDto.Status, articles[0].Status); Assert.AreEqual(1, articles[0].ArticleTags.Count); Assert.AreEqual(".net", articles[0].ArticleTags[0].Tag.Name); Assert.AreEqual(1, totalTags); }
public async Task <ArticleDetailDto> Post([FromBody] PostArticleDto dto) { return(await _articleService.PostAsync(dto)); }