Esempio n. 1
0
        public async Task GetByCategoryIdShouldReturnCorrectPostsUsingDbContext()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var repository = new EfDeletableEntityRepository <Post>(new ApplicationDbContext(options.Options));

            repository.AddAsync(new Post {
                Id = 3, CategoryId = 1, Title = "test"
            }).GetAwaiter().GetResult();
            repository.AddAsync(new Post {
                Id = 4, CategoryId = 1, Title = "b"
            }).GetAwaiter().GetResult();
            repository.AddAsync(new Post {
                Id = 5, CategoryId = 2, Title = "c"
            }).GetAwaiter().GetResult();
            repository.AddAsync(new Post {
                Id = 6, CategoryId = 2, Title = "d"
            }).GetAwaiter().GetResult();
            repository.SaveChangesAsync().GetAwaiter().GetResult();
            var postService = new PostsService(repository);

            AutoMapperConfig.RegisterMappings(typeof(MyTestPost).Assembly);
            var posts = postService.GetByCategoryId <MyTestPost>(1);

            Assert.Equal(2, posts.Count());
        }
Esempio n. 2
0
        public async void TestGetByCategoryId()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var repository  = new EfDeletableEntityRepository <Post>(new ApplicationDbContext(options.Options));
            var postService = new PostsService(repository);
            var userId      = Guid.NewGuid().ToString();
            await postService.CreateAsync("test", "test", 2, userId);

            await postService.CreateAsync("test1", "test1", 2, userId);

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

            Assert.Equal(2, postService.GetByCategoryId <MyTestPost>(2, 2).Count());
        }
Esempio n. 3
0
        public void GetByCategoryIdShouldReturnAllPostsInCategory()
        {
            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.GetByCategoryId <PostViewModel>(1);

            Assert.Equal(2, result.Count());
        }