public async Task GetCommentByIdAsyncShouldReturnCommentSuccessfully() { var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) .Options; var dbContext = new MyCalisthenicAppDbContext(options); IHttpContextAccessor httpContextAccessor = new HttpContextAccessor(); var usersService = new UsersService(httpContextAccessor, dbContext, null); var mockMapper = new MapperConfiguration(cfg => { cfg.AddProfile(new MyCalisthenicAppProfile()); }); var mapper = mockMapper.CreateMapper(); var commentsService = new CommentsService(dbContext, mapper, usersService); var user = new ApplicationUser { FirstName = UserFirstName, LastName = UserLastName, }; await dbContext.Users.AddAsync(user); await dbContext.SaveChangesAsync(); var comment = new Comment { Text = CommentText, AuthorId = user.Id, }; await dbContext.Comments.AddAsync(comment); await dbContext.SaveChangesAsync(); var expected = await commentsService.GetCommentByIdAsync(comment.Id); Assert.Equal(expected.Id, comment.Id); }
public async Task GetCommentByIdAsyncShouldThrowExeptionIfCommentIsNull() { var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) .Options; var dbContext = new MyCalisthenicAppDbContext(options); IHttpContextAccessor httpContextAccessor = new HttpContextAccessor(); var usersService = new UsersService(httpContextAccessor, dbContext, null); var mockMapper = new MapperConfiguration(cfg => { cfg.AddProfile(new MyCalisthenicAppProfile()); }); var mapper = mockMapper.CreateMapper(); var commentsService = new CommentsService(dbContext, mapper, usersService); var exception = await Assert.ThrowsAsync <ArgumentNullException>(async() => await commentsService.GetCommentByIdAsync(CommentId)); Assert.IsType <ArgumentNullException>(exception); }
public async Task <IActionResult> GetDetails(string slug, string commentId, CreateOrEditCommentDto model) { Comment comment = await _commentService.GetCommentByIdAsync(commentId); return(StatusCodeAndDtoWrapper.BuildSuccess(CommentDetailsDto.Build(comment))); }