public async Task GivenValidRequest_WhenTheUserDidNotCreateTheArticle_ThrowsConduitApiExceptionForBadRequest() { // Arrange var stubArticleDto = new UpdateArticleDto { Body = "This isn't my article!", Description = "Or is it?", Title = "I'm a hacker trying to hack hacky articles about hacking." }; var updateArticleCommand = new UpdateArticleCommand { Slug = "how-to-train-your-dragon", Article = stubArticleDto }; // Act var request = new UpdateArticleCommandHandler(_logger, Context, CurrentUserContext, Mapper, MachineDateTime); // Assert await Should.ThrowAsync <ConduitApiException>(async() => { await request.Handle(updateArticleCommand, CancellationToken.None); }); }
public async Task GivenValidRequest_WhenTheArticleSlugIsNotFound_ThrowsConduitApiExceptionForNotFound() { // Arrange var stubArticleDto = new UpdateArticleDto { Body = "I don't exist", Description = "Or do I?", Title = "What is Life? 42." }; var updateArticleCommand = new UpdateArticleCommand { Slug = "this-article-does-not-exist", Article = stubArticleDto }; // Act var request = new UpdateArticleCommandHandler(_logger, Context, CurrentUserContext, Mapper, MachineDateTime); // Assert await Should.ThrowAsync <ConduitApiException>(async() => { await request.Handle(updateArticleCommand, CancellationToken.None); }); }
public async Task GivenValidRequest_WhenASubsetOfPropertiesIsSent_ReturnsUpdatedArticleResponseWithOriginalPropertiesUnchanged() { // Arrange, retrieve the original article information var stubArticleDto = new UpdateArticleDto { Title = "I'm a hacker trying to hack hacky articles about hacking." }; var updateArticleCommand = new UpdateArticleCommand { Slug = "why-beer-is-gods-gift-to-the-world", Article = stubArticleDto }; // Act var request = new UpdateArticleCommandHandler(_logger, Context, CurrentUserContext, Mapper, MachineDateTime); var result = await request.Handle(updateArticleCommand, CancellationToken.None); // Assert var updatedArticle = Context.Articles.FirstOrDefault(a => string.Equals(a.Slug, stubArticleDto.Title.ToSlug(), StringComparison.OrdinalIgnoreCase)); updatedArticle.ShouldNotBeNull(); result.ShouldNotBeNull(); result.ShouldBeOfType <ArticleViewModel>(); result.Article.Body.ShouldBe(updatedArticle.Body); result.Article.Description.ShouldBe(updatedArticle.Description); result.Article.Title.ShouldBe(stubArticleDto.Title); result.Article.TagList.ShouldContain("beer"); // Validate the article in the DB context updatedArticle.Body.ShouldBe(updatedArticle.Body); updatedArticle.Description.ShouldBe(updatedArticle.Description); updatedArticle.Title.ShouldBe(stubArticleDto.Title); }
public async Task UpdateArticleCommandTestAsync(int articleId, IEnumerable <string> userRoles, string title, bool isPublic, string titleImagePath, int categoryId, UpdateArticleResponse result) { UpdateArticleCommand request = new UpdateArticleCommand { ArticleId = articleId, Title = title, TitleImagePath = titleImagePath, UserRoles = userRoles, CategoryId = categoryId, IsPublic = isPublic, }; UpdateArticleCommandHandler handler = new UpdateArticleCommandHandler(_updateFixture.Context); var expectedResult = await handler.Handle(request, new CancellationToken()); Assert.Equal(expectedResult.IsSuccessful, result.IsSuccessful); if (expectedResult.IsSuccessful) { Article article = await _updateFixture.Context.Articles .Include(a => a.TitleImage) .Include(a => a.Creator) .Where(a => a.Id == expectedResult.ArticleId).SingleOrDefaultAsync(); Assert.NotNull(article); Assert.Equal(article.Title, title); Assert.Equal(article.TitleImage.Path, titleImagePath); Assert.Equal(article.CategoryId, categoryId); Assert.Equal(article.IsPublic, isPublic); } Assert.Equal(expectedResult.Message, result.Message); }
public async Task UpdateArticleFail_When_Article_Does_Not_Exist() { var updateArticleCommand = new UpdateArticleCommand { Id = nonExistingArticleId, }; var sut = new UpdateArticleCommandHandler(db); await Assert.ThrowsAsync <DbUpdateConcurrencyException>(() => sut.Handle(updateArticleCommand, CancellationToken.None)); }
public async Task UpdateArticleSuccessTest() { var queryHandler = new GetArticleQueryHandler(db); var getArticleQuery = new GetArticleQuery(firstArticleId); var article = await queryHandler.Handle(getArticleQuery, CancellationToken.None); db = new AppDatabaseContext(options); var sut = new UpdateArticleCommandHandler(db); var updatedArticleCommand = new UpdateArticleCommand { Id = firstArticleId, Title = "Updated Article Title" }; var result = await sut.Handle(updatedArticleCommand, CancellationToken.None); var updatedArticle = queryHandler.Handle(getArticleQuery, CancellationToken.None); Assert.IsType <Unit>(result); Assert.NotNull(updatedArticle); }
void GivenSystemUnderTest() { _sut = new UpdateArticleCommandHandler(_articleRepository); }