public async Task GivenValidArticleRequest_WhenTheRequestDoesNotContainTagList_ReturnsSuccessfulResponseWithViewModel() { // Arrange var createArticleCommand = new CreateArticleCommand { Article = new CreateArticleDto { Title = "Why C# is the Best Language", Description = "It really is!", Body = "I love .NET Core", } }; // Act var request = new CreateArticleCommandHandler(CurrentUserContext, Context, _logger, Mapper, new DateTimeTest()); var result = await request.Handle(createArticleCommand, CancellationToken.None); // Assert the response model result.ShouldNotBeNull(); result.ShouldBeOfType <ArticleViewModel>(); result.Article.ShouldNotBeNull(); result.Article.Slug.ShouldBe("why-c-is-the-best-language"); result.Article.FavoritesCount.ShouldBe(0); result.Article.TagList.ShouldBeEmpty(); result.Article.Author.Username.ShouldBe(TestConstants.TestUserName); // Assert the article within the DB context var newArticle = Context.Articles.FirstOrDefault(a => string.Equals(a.Slug, result.Article.Slug, StringComparison.OrdinalIgnoreCase)); newArticle.ShouldNotBeNull(); newArticle.Slug.ShouldBe("why-c-is-the-best-language"); newArticle.FavoritesCount.ShouldBe(0); newArticle.Author.Email.ShouldBe(TestConstants.TestUserEmail); Context.ArticleTags.SingleOrDefault(at => at.ArticleId == newArticle.Id)?.ShouldBeNull(); }
public async Task CreateArticleCommandTestAsync(string title, string titleImagePath, string identityUserId, int categoryId, bool isPublic, CreateArticleResponse result) { CreateArticleCommand request = new CreateArticleCommand { Title = title, TitleImagePath = titleImagePath, IdentityUserId = identityUserId, CategoryId = categoryId, IsPublic = isPublic, }; CreateArticleCommandHandler handler = new CreateArticleCommandHandler(_createFixture.Context); var expectedResult = await handler.Handle(request, new CancellationToken()); Assert.Equal(expectedResult.IsSuccessful, result.IsSuccessful); if (expectedResult.IsSuccessful) { Article article = await _createFixture.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.Creator.IdentityUserId, identityUserId); Assert.Equal(article.CategoryId, categoryId); Assert.Equal(article.IsPublic, isPublic); } Assert.Equal(expectedResult.Message, result.Message); }
public void ShouldNotCallHandleIfNotSavedChanges() { context.Setup(x => x.Articles).Returns(dbSetArticle.Object); context.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(0)); CreateArticleCommandHandler createArticleCommandHandler = new CreateArticleCommandHandler(context.Object, stringLocalizer.Object, mapper.Object); CreateArticleCommand createArticleCommand = new CreateArticleCommand(); mapper.Setup(x => x.Map <Article>(createArticleCommand)).Returns(new Article()); Func <Task> act = async() => await createArticleCommandHandler.Handle(createArticleCommand, new CancellationToken()); act.Should().Throw <RestException>(); }
public async Task CreateArticleSuccessTest() { //test main branch var sut = new CreateArticleCommandHandler(db); var createArticleCommand = new CreateArticleCommand { Title = newTitleArticle, }; var result = await sut.Handle(createArticleCommand, CancellationToken.None); var createdArticle = db.Articles.Where(u => u.Title == newTitleArticle); Assert.IsType <Unit>(result); Assert.NotNull(createdArticle); }
public async Task ShouldCallHandle() { var id = new Guid(); var articleDto = new ArticleCreateOrEditDto { Id = id }; var article = new Article { Id = id }; context.Setup(x => x.Articles).Returns(dbSetArticle.Object); context.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(1)); CreateArticleCommandHandler createArticleCommandHandler = new CreateArticleCommandHandler(context.Object, stringLocalizer.Object, mapper.Object); CreateArticleCommand createArticleCommand = new CreateArticleCommand(articleDto); mapper.Setup(x => x.Map <Article>(createArticleCommand)).Returns(article); var result = await createArticleCommandHandler.Handle(createArticleCommand, new CancellationToken()); result.Should().Be(id); }
public void Handle() { var mediatorMock = new Mock <IMediator>(); var userManager = var context = new Mock <AppDBContext>(); userManager.Object.CreateAsync(new AppUser() { Email = "*****@*****.**" }, "Belazlol123@"); var sut = new CreateArticleCommandHandler(context.Object, userManager.Object); context.Object.SaveChanges(); // Act var result = sut.Handle(new CreateArticleViewModel { AppUserId = userManager.Object.FindByEmailAsync("*****@*****.**").Result.Id, Content = "fsdf", Tags = "#fsd#fsd", Title = "gfdgdf" }, CancellationToken.None); // Assert Action.ReferenceEquals(result.Result, Unit.Value); }
async Task WhenHandlerHandlesTheCommand() { await _sut.Handle(_command); }