public void AnswerOnComment_InValidGameId_ThrowsArgumentException() { //assing var comment = new CreateAnswerCommentDto() { GameId = 2, ParentCommentId = 1 }; Game returnedNullGame = null; Comment commentEntity = new Comment() { Id = 1 }; commentRepositoryMoq.Setup((p) => p.GetSingleAsync(It.IsAny <Expression <Func <Comment, bool> > >())) .Returns(Task.FromResult(commentEntity)); gameRepositoryMoq.Setup((p) => p.GetSingleAsync(It.IsAny <Expression <Func <Game, bool> > >())) .Returns(Task.FromResult(returnedNullGame)); //act Assert.ThrowsAsync <ArgumentException>(async() => { await service.AnswerOnComment(comment); }); //assert unitofworkMoq.Verify(p => p.CommitAsync(), Times.Never); }
public async Task AnswerOnComment(CreateAnswerCommentDto comment) { using (unitOfWork) { var commentOnAnswer = unitOfWork.CommentRepository.GetSingleAsync(c => c.Id == comment.ParentCommentId); var game = unitOfWork.GameRepository.GetSingleAsync(c => c.Id == comment.GameId); if (await commentOnAnswer == null) { throw new ArgumentException("Invalid comment id"); } if (await game == null) { throw new ArgumentException("Invalid game id"); } unitOfWork.CommentRepository.Create(new Comment() { Name = commentOnAnswer.Result. Body = $"{commentOnAnswer.Result.Name}, " + comment.Body, ParentComment = commentOnAnswer.Result, Game = game.Result, }); await unitOfWork.CommitAsync(); } }
public async Task <IActionResult> CreateAnswerForAnotherComment(int id, [FromBody] CreateAnswerModel comment) { var commentDto = new CreateAnswerCommentDto() { ParentCommentId = id, Body = comment.Body, GameId = comment.GameId, Name = comment.Name }; await commnetService.AnswerOnComment(commentDto); return(StatusCode((int)HttpStatusCode.Created, "Comment was created")); }
public void AnswerOnComment_MethodParameterIsNull_ThrowsArgumetnException() { //assing CreateAnswerCommentDto comment = null; Game gameEntity = null; Comment nullCommentEntity = null; commentRepositoryMoq.Setup((p) => p.GetSingleAsync(It.IsAny <Expression <Func <Comment, bool> > >())) .Returns(Task.FromResult(nullCommentEntity)); gameRepositoryMoq.Setup((p) => p.GetSingleAsync(It.IsAny <Expression <Func <Game, bool> > >())) .Returns(Task.FromResult(gameEntity)); //act Assert.ThrowsAsync <ArgumentException>(async() => { await service.AnswerOnComment(comment); }); //assert unitofworkMoq.Verify(p => p.CommitAsync(), Times.Never); }