public void ShouldNotCallHandleIfPostNotExist() { dbSetPost.Setup(x => x.FindAsync(id)).Returns(null); context.Setup(x => x.Posts).Returns(dbSetPost.Object); DeletePostCommandHandler deletePostCommandHandler = new DeletePostCommandHandler(context.Object, stringLocalizer.Object); DeletePostCommand deletePostCommand = new DeletePostCommand(id); Func <Task> act = async() => await deletePostCommandHandler.Handle(deletePostCommand, new CancellationToken()); act.Should().Throw <NotFoundException>(); }
public void ShouldNotCallHandleIfNotSavedChanges() { dbSetPost.Setup(x => x.FindAsync(id)).Returns(new ValueTask <Post>(Task.FromResult(post))); context.Setup(x => x.Posts).Returns(dbSetPost.Object); context.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(0)); DeletePostCommandHandler deletePostCommandHandler = new DeletePostCommandHandler(context.Object, stringLocalizer.Object); DeletePostCommand deletePostCommand = new DeletePostCommand(id); Func <Task> act = async() => await deletePostCommandHandler.Handle(deletePostCommand, new CancellationToken()); act.Should().Throw <RestException>(); }
public async Task ShouldCallHandle() { dbSetPost.Setup(x => x.FindAsync(id)).Returns(new ValueTask <Post>(Task.FromResult(post))); context.Setup(x => x.Posts).Returns(dbSetPost.Object); context.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(1)); DeletePostCommandHandler deletePostCommandHandler = new DeletePostCommandHandler(context.Object, stringLocalizer.Object); DeletePostCommand deletePostCommand = new DeletePostCommand(id); var result = await deletePostCommandHandler.Handle(deletePostCommand, new CancellationToken()); result.Should().Be(Unit.Value); }
public async Task GivenInvalidRequest_ShouldThrowNotFoundException() { // Arrange var sut = new DeletePostCommandHandler(_context); var postId = 10; // Act var ex = await Assert.ThrowsAsync <NotFoundException>(() => sut.Handle(new DeletePostCommand { Id = postId }, CancellationToken.None)); // Assert Assert.IsType <NotFoundException>(ex); }
public void Should_call_save_method_when_delete_post() { var postId = Guid.NewGuid(); var post = Post.CreateNew(Guid.NewGuid(), "Title", "Content", true, new List <Guid>(), new List <string>()); post.GetType().GetProperty("Id").SetValue(post, postId, null); var postRepository = new Mock <IPostRepository>(); postRepository.Setup(x => x.GetById(postId)).Returns(post); postRepository.Setup(x => x.Save(post)); var deletePostCommandHandler = new DeletePostCommandHandler(postRepository.Object); deletePostCommandHandler.Handle(new DeletePostCommand(postId)); postRepository.Verify(x => x.Save(post)); }
public void GivenValidRequest_ShouldSuccessfullyDeletePost() { // Arrange var sut = new DeletePostCommandHandler(_context); var entity = new Post("Test Post 1", "Description", "Content", 1, null, true); _context.Posts.Add(entity); _context.SaveChanges(); // Act var result = sut.Handle(new DeletePostCommand { Id = entity.Id }, CancellationToken.None); var post = _context.Posts.FirstOrDefault(i => i.Id == entity.Id); // Assert Assert.IsType <Unit>(result.Result); Assert.Null(post); }