Exemplo n.º 1
0
        public async Task UpdateCategoryCommandTestAsync(string identityUserId, int categoryId, string title, string titleImagePath, CreateCategoryResponse result)
        {
            UpdateCategoryCommand request = new UpdateCategoryCommand
            {
                CategoryId     = categoryId,
                IdentityUserId = identityUserId,
                Title          = title,
                TitleImagePath = titleImagePath,
            };
            UpdateCategoryCommandHandler handler = new UpdateCategoryCommandHandler(_updateFixture.Context);
            var expectedResult = await handler.Handle(request, new CancellationToken());

            Assert.Equal(expectedResult.IsSuccessful, result.IsSuccessful);
            if (expectedResult.IsSuccessful)
            {
                Category category = await _updateFixture.Context.Categories
                                    .Include(c => c.Creator)
                                    .Include(c => c.TitleImage)
                                    .Where(c => c.Id == expectedResult.Id)
                                    .SingleOrDefaultAsync();

                Assert.Equal(category.Title, title);
                Assert.Equal(category.TitleImage.Path, titleImagePath);
                Assert.Equal(category.Creator.IdentityUserId, identityUserId);
            }
            Assert.Equal(expectedResult.Message, result.Message);
        }
        public async Task Update_Category()
        {
            using (var context = GetContextWithData())
            {
                var handler = new UpdateCategoryCommandHandler(context);
                var command = new UpdateCategoryCommand
                {
                    Id   = (await context.Categories.FirstOrDefaultAsync()).Id,
                    Name = "Test2"
                };

                await handler.Handle(command, CancellationToken.None);

                Assert.Equal(command.Name, (await context.Categories.FindAsync(command.Id)).Name);
            }
        }
Exemplo n.º 3
0
        public async Task GivenInvalidRequest_ShouldThrowNotFoundException()
        {
            // Arrange
            var sut        = new UpdateCategoryCommandHandler(_context);
            var categoryId = 10;

            // Act
            var ex = await Assert.ThrowsAsync <NotFoundException>(() => sut.Handle(new UpdateCategoryCommand
            {
                Id         = categoryId,
                Name       = "",
                UpdateSlug = false
            },
                                                                                   CancellationToken.None));

            // Assert
            Assert.IsType <NotFoundException>(ex);
        }
Exemplo n.º 4
0
        public async Task UpdateCategoryHandler_Updates_Category()
        {
            var category = await GetRandomCategory();

            var message = new UpdateCategoryCommand()
            {
                Id = category.Id, Name = "New name", Description = "New description"
            };
            var handler = new UpdateCategoryCommandHandler(RequestDbContext);

            var result = await handler.Handle(message, CancellationToken.None);

            Assert.IsType <SuccessResult>(result);

            using (var context = TestContext.CreateNewContext())
            {
                var updatedCategory = await context.Categories.FindAsync(category.Id);

                Assert.Equal("New name", updatedCategory.Name);
                Assert.Equal("New description", updatedCategory.Description);
            }
        }
Exemplo n.º 5
0
        public void GivenValidRequest_ShouldUpdateCategoryCorrectly()
        {
            // Arrange
            var sut    = new UpdateCategoryCommandHandler(_context);
            var entity = new Category("Update Category 1");

            _context.Categories.Add(entity);
            _context.SaveChanges();

            // Act
            var result = sut.Handle(new UpdateCategoryCommand
            {
                Id         = entity.Id,
                Name       = "New Update Category 1 Name",
                UpdateSlug = true
            },
                                    CancellationToken.None);

            Assert.IsType <Unit>(result.Result);
            Assert.True(entity.Id > 0);
            Assert.Equal("New Update Category 1 Name", entity.Name);
            Assert.Equal("new-update-category-1-name", entity.Slug);
            Assert.Empty(entity.Posts);
        }