コード例 #1
0
        public async Task <IActionResult> SayThanks([FromBody] CreateThankModel model, CancellationToken cancellationToken)
        {
            var resultValidation = await _createThankModelValidator.ValidateAsync(model, cancellationToken);

            resultValidation.AddToModelState(ModelState, null);

            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity());
            }

            await _thankService.AddAsync(model, UserId, cancellationToken);

            return(Ok());
        }
コード例 #2
0
        public async Task AddAsync_ValidCreateThankModel(CreateThankModel model)
        {
            //Arrange
            var cancellationToken = new CancellationToken();

            var user = UserDumbData.GetRandomEntity();

            _userRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(user));
            _thankRepository.Setup(x => x.AddAsync(It.IsAny <Thank>(), cancellationToken)).Returns(Task.CompletedTask);
            _unitOfWork.Setup(x => x.SaveChangesAsync(cancellationToken)).Returns(Task.CompletedTask);

            // Act
            await _thankService.AddAsync(model, user.Id, cancellationToken);

            // Assert
            _userRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once);
            _thankRepository.Verify(x => x.AddAsync(It.IsAny <Thank>(), cancellationToken), Times.Once);
            _unitOfWork.Verify(x => x.SaveChangesAsync(cancellationToken), Times.Once);
        }