public async Task GivenValidRequest_ShouldCreateCorrectTagEntity() { // Arrange var sut = new CreatePostCommandHandler(_context, _mapper); // Act var result = await sut.Handle(new CreatePostCommand { Title = "New Post 1", Description = "Test Post Description", Content = "Test Post Content", CategoryId = 1, Tags = null, Published = true }, CancellationToken.None); // Assert Assert.IsType <PostDetailDto>(result); Assert.Equal("new-post-1", result.Slug); Assert.Equal("Test Post Description", result.Description); Assert.Equal("Test Post Content", result.Content); Assert.Empty(result.Tags); Assert.True(result.Created < DateTime.UtcNow); }
public async Task shouldReturnTheSavedPost() { //Arrange var commandHandler = new CreatePostCommandHandler(fakeRepo); //Act var result = await commandHandler.Handle(command, cancellationToken); //Assert Assert.Equal(post.Title.ToString(), result.Title.ToString()); Assert.Equal(post.CreationDate, result.CreationDate); Assert.Equal(post.Content, result.Content); }
public async Task shouldCallSavePost() { //Arrange fakeRepo = A.Fake <IRepository <Post> >(); var commandHandler = new CreatePostCommandHandler(fakeRepo); //Act var result = await commandHandler.Handle(command, cancellationToken); //Assert A.CallTo(() => fakeRepo.Save(A <Post> ._)).MustHaveHappened(); }
public void ShouldNotCallHandleIfDiscussionNotExist() { dbSetDiscussion.Setup(x => x.FindAsync(id)).Returns(null); context.Setup(x => x.Posts).Returns(dbSetPost.Object); context.Setup(x => x.Discussions).Returns(dbSetDiscussion.Object); CreatePostCommandHandler createPostCommandHandler = new CreatePostCommandHandler(context.Object, stringLocalizer.Object, mapper.Object); CreatePostCommand createPostCommand = new CreatePostCommand(postDto); Func <Task> act = async() => await createPostCommandHandler.Handle(createPostCommand, new CancellationToken()); act.Should().Throw <NotFoundException>(); }
public void shouldThrowExceptionIfRepositoryIsNull() { //Arrange IRequestHandler <CreatePostCommand, Post> createPostCommandHandler; fakeRepo = null; //Act void action() => createPostCommandHandler = new CreatePostCommandHandler(fakeRepo); //Assert Assert.Throws <ArgumentNullException>(action); }
public async Task ShouldCallHandle() { dbSetDiscussion.Setup(x => x.FindAsync(id)).Returns(new ValueTask <Discussion>(Task.FromResult(discussion))); context.Setup(x => x.Posts).Returns(dbSetPost.Object); context.Setup(x => x.Discussions).Returns(dbSetDiscussion.Object); context.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(1)); CreatePostCommandHandler createPostCommandHandler = new CreatePostCommandHandler(context.Object, stringLocalizer.Object, mapper.Object); CreatePostCommand createPostCommand = new CreatePostCommand(postDto); mapper.Setup(x => x.Map <Post>(createPostCommand)).Returns(post); var result = await createPostCommandHandler.Handle(createPostCommand, new CancellationToken()); result.Should().Be(id); }
public void ShouldNotCallHandleIfNotSavedChanges() { dbSetDiscussion.Setup(x => x.FindAsync(id)).Returns(new ValueTask <Discussion>(Task.FromResult(discussion))); context.Setup(x => x.Posts).Returns(dbSetPost.Object); context.Setup(x => x.Discussions).Returns(dbSetDiscussion.Object); context.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(0)); CreatePostCommandHandler createPostCommandHandler = new CreatePostCommandHandler(context.Object, stringLocalizer.Object, mapper.Object); CreatePostCommand createPostCommand = new CreatePostCommand(postDto); mapper.Setup(x => x.Map <Post>(createPostCommand)).Returns(post); Func <Task> act = async() => await createPostCommandHandler.Handle(createPostCommand, new CancellationToken()); act.Should().Throw <RestException>(); }
public void Should_call_validate_command_when_create_new_post() { var post = Post.CreateNew(Guid.NewGuid(), "Title", "Content", false, new List <Guid>(), new List <string>()); var command = new CreatePostCommand(post.BlogId, post.Title, post.Content, post.Published, post.Categories, post.Tags); var postRepository = new Mock <IPostRepository>(); var validator = new Mock <IValidator <CreatePostCommand> >(); validator.Setup(x => x.Validate(command)).Returns(new ValidationResult()); var createPostCommandHandler = new CreatePostCommandHandler(postRepository.Object, validator.Object); createPostCommandHandler.Handle(command); validator.Verify(x => x.Validate(command)); }