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);
        }
Пример #4
0
        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);
        }
Пример #5
0
        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));
        }
Пример #6
0
        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);
        }
        public void TestInitialize()
        {
            this.requesterSecurity = new Mock <IRequesterSecurity>();
            this.requesterSecurity.SetupFor(Requester);
            this.postSecurity = new Mock <IPostSecurity>();

            this.timestampCreator = new Mock <ITimestampCreator>();
            this.timestampCreator.Setup(v => v.Now()).Returns(Now);

            this.deletePost = new Mock <IDeletePostDbStatement>(MockBehavior.Strict);
            this.defragmentQueueIfRequired = new Mock <IDefragmentQueueIfRequiredDbStatement>(MockBehavior.Strict);
            this.scheduleGarbageCollection = new Mock <IScheduleGarbageCollectionStatement>(MockBehavior.Strict);

            this.target = new DeletePostCommandHandler(
                this.requesterSecurity.Object,
                this.postSecurity.Object,
                this.deletePost.Object,
                this.defragmentQueueIfRequired.Object,
                this.scheduleGarbageCollection.Object,
                this.timestampCreator.Object);
        }