Beispiel #1
0
        public async Task GetCountMethodShouldCountAllNotDeletedPosts()
        {
            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 posts = new List <Post>
            {
                new Post
                {
                    Id          = 1,
                    Title       = "Test title 1",
                    Description = "Test description 1",
                    Type        = PostType.Discussion,
                    CategoryId  = 1,
                    AuthorId    = guid,
                    CreatedOn   = dateTimeProvider.Object.Now()
                },
                new Post
                {
                    Id          = 2,
                    Title       = "Test title 2",
                    Description = "Test description 2",
                    Type        = PostType.Discussion,
                    CategoryId  = 2,
                    AuthorId    = guid,
                    CreatedOn   = dateTimeProvider.Object.Now()
                },
                new Post
                {
                    Id          = 3,
                    Title       = "Test title 3",
                    Description = "Test description 3",
                    Type        = PostType.Discussion,
                    CategoryId  = 2,
                    AuthorId    = guid,
                    IsDeleted   = true,
                    CreatedOn   = dateTimeProvider.Object.Now(),
                    DeletedOn   = dateTimeProvider.Object.Now()
                }
            };

            await db.Posts.AddRangeAsync(posts);

            await db.SaveChangesAsync();

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

            var count = await postsService.GetCountAsync();

            count.Should().Be(2);
        }