Exemplo n.º 1
0
        public async Task GetCountByCategoryIdShouldReturnCorrectNumberUsingDbContext()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "PostsTestDb").Options;
            var dbContext = new ApplicationDbContext(options);

            dbContext.Posts.Add(new Post()
            {
                CategoryId = 3
            });
            dbContext.Posts.Add(new Post()
            {
                CategoryId = 3
            });
            dbContext.Posts.Add(new Post()
            {
                CategoryId = 3
            });
            await dbContext.SaveChangesAsync();

            var repository = new EfDeletableEntityRepository <Post>(dbContext);
            var service    = new PostsService(repository);

            Assert.Equal(3, service.GetCountByCategoryId(3));
        }
Exemplo n.º 2
0
        public void GetCountByCategoryIdShouldReturnCorrectCount()
        {
            var mockRepo = new Mock <IDeletableEntityRepository <Post> >();

            mockRepo.Setup(x => x.All())
            .Returns(new List <Post>()
            {
                new Post
                {
                    Content    = "Test",
                    Title      = "Test",
                    CategoryId = 1,
                    Category   = new Category(),
                    UserId     = "Test",
                    User       = new ApplicationUser(),
                },
                new Post
                {
                    Content    = "Test2",
                    Title      = "Test2",
                    CategoryId = 1,
                    Category   = new Category(),
                    UserId     = "Test2",
                    User       = new ApplicationUser(),
                },
                new Post
                {
                    Content    = "Test3",
                    Title      = "Test3",
                    CategoryId = 2,
                    Category   = new Category(),
                    UserId     = "Test3",
                    User       = new ApplicationUser(),
                },
            }.AsQueryable());

            var service = new PostsService(mockRepo.Object);

            var result  = service.GetCountByCategoryId(1);
            var result2 = service.GetCountByCategoryId(2);

            Assert.Equal(2, result);
            Assert.Equal(1, result2);
        }
Exemplo n.º 3
0
        public async void TestGetCountByCategoryId()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var repository  = new EfDeletableEntityRepository <Post>(new ApplicationDbContext(options.Options));
            var postService = new PostsService(repository);

            await postService.CreateAsync("test", "test", 2, Guid.NewGuid().ToString());

            AutoMapperConfig.RegisterMappings(typeof(MyTestPost).Assembly);
            var post = postService.GetById <MyTestPost>(1);

            Assert.Equal(1, postService.GetCountByCategoryId(2));
        }
Exemplo n.º 4
0
        public void GetCountByCategoryIdShouldReturnCorrectNumber()
        {
            var repository = new Mock <IDeletableEntityRepository <Post> >();

            repository.Setup(r => r.All()).Returns(new List <Post>
            {
                new Post()
                {
                    CategoryId = 3
                },
                new Post()
                {
                    CategoryId = 3
                },
                new Post()
                {
                    CategoryId = 3
                },
            }.AsQueryable());
            var service = new PostsService(repository.Object);

            Assert.Equal(3, service.GetCountByCategoryId(3));
            repository.Verify(x => x.All(), Times.Once);
        }