コード例 #1
0
        public async Task DeleteAsyncShouldThrowNullExceptionForNotFound()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "DeleteAsyncShouldThrowNullExceptionForNotFound").Options;
            var dbContext = new ApplicationDbContext(options);

            var postsRepo    = new EfDeletableEntityRepository <Post>(dbContext);
            var makesRepo    = new EfDeletableEntityRepository <Make>(dbContext);
            var makesService = new MakesService(makesRepo);
            var postsService = new PostsService(postsRepo, makesService);

            await Assert.ThrowsAsync <ArgumentNullException>(() => postsService.DeleteAsync(1));
        }
コード例 #2
0
        public async Task DeleteAsync_WithValidData_ShouldReturnTrue()
        {
            // Arrange
            var context        = InMemoryDbContext.Initiliaze();
            var postRepository = new EfDeletableEntityRepository <Post>(context);
            var postService    = new PostsService(postRepository);

            await postRepository.AddAsync(new Post { Id = 52, CreatorId = "testId", MediaSource = "dsadsa" });

            await postRepository.SaveChangesAsync();

            // Act
            var result = await postService.DeleteAsync(52);

            // Assert
            Assert.True(result);
        }
コード例 #3
0
        public async Task DeleteAsyncShouldWork()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "DeleteAsyncShouldWork").Options;
            var dbContext = new ApplicationDbContext(options);

            var postsRepo    = new EfDeletableEntityRepository <Post>(dbContext);
            var makesRepo    = new EfDeletableEntityRepository <Make>(dbContext);
            var makesService = new MakesService(makesRepo);
            var postsService = new PostsService(postsRepo, makesService);

            await postsService.AddAsync(new Post());

            Assert.Single(postsService.GetAll());
            await postsService.DeleteAsync(1);

            Assert.Empty(postsService.GetAll());
        }
コード例 #4
0
        public async Task DeleteMethodShouldSetDeletedOnToCurrentDateAndIsDeletedToTrue(string title, PostType type, string description, int categoryId)
        {
            var guid = Guid.NewGuid().ToString();

            var options = new DbContextOptionsBuilder <ForumDbContext>()
                          .UseInMemoryDatabase(guid)
                          .Options;

            var db = new ForumDbContext(options);
            var dateTimeProvider = new Mock <IDateTimeProvider>();

            dateTimeProvider.Setup(dtp => dtp.Now()).Returns(new DateTime(2020, 3, 27));

            var post = new Post
            {
                Id          = 1,
                Title       = title,
                Description = description,
                Type        = type,
                CreatedOn   = dateTimeProvider.Object.Now(),
                CategoryId  = categoryId,
                AuthorId    = guid,
                Tags        = new List <PostTag>
                {
                    new PostTag {
                        TagId = 1
                    }
                }
            };

            await db.Posts.AddAsync(post);

            await db.SaveChangesAsync();

            var postsService = new PostsService(db, null, null, dateTimeProvider.Object);

            await postsService.DeleteAsync(1);

            var actual = await db.Posts.FirstOrDefaultAsync();

            actual.IsDeleted.Should().BeTrue();
            actual.DeletedOn.Should().BeSameDateAs(dateTimeProvider.Object.Now());
        }
コード例 #5
0
        public async Task DeleteAsync_WithValidData_ShouldRemovePostFromDatabase()
        {
            // Arrange
            var context        = InMemoryDbContext.Initiliaze();
            var postRepository = new EfDeletableEntityRepository <Post>(context);
            var postService    = new PostsService(postRepository);

            await postRepository.AddAsync(new Post { Id = 52, CreatorId = "testId", MediaSource = "dsadsa" });

            await postRepository.SaveChangesAsync();

            // Act
            await postService.DeleteAsync(52);

            int expectedCount = 0;
            int actualCount   = postRepository.All().Count();

            // Assert
            Assert.Equal(expectedCount, actualCount);
        }