예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        public async Task <IActionResult> GetDetails(string slug, string commentId, CreateOrEditCommentDto model)
        {
            Comment comment = await _commentService.GetCommentByIdAsync(commentId);

            return(StatusCodeAndDtoWrapper.BuildSuccess(CommentDetailsDto.Build(comment)));
        }