예제 #1
0
        public async Task GetPostById_ReturnsOk()
        {
            // Arrange
            var post = GenerateFake.Post();

            _mockRepo.Setup(repo => repo.GetPostById(post.Id))
            .ReturnsAsync(post);

            // Act
            var response = await _postController.GetPostById(post.Id);

            // Assert
            Assert.IsAssignableFrom <ObjectResult>(response);
        }
예제 #2
0
        public async Task GetPosts_ReturnsObject()
        {
            // Arrange
            IList <Post> posts = new List <Post> {
                GenerateFake.Post(),
                         GenerateFake.Post(),
                         GenerateFake.Post()
            };

            _mockRepo.Setup(repo => repo.GetPosts())
            .ReturnsAsync(posts);

            // Act
            var response = await _postController.GetPosts();

            // Assert
            Assert.IsAssignableFrom <ObjectResult>(response);
        }
예제 #3
0
        public async void GetPosts_PostsAmount_ReturnsCorrectAmountOfPosts(int expectedAmountPosts)
        {
            //Arrange
            ILoggerFactory           loggerFactory = new LoggerFactory();
            ILogger <PostRepository> logger        = loggerFactory.CreateLogger <PostRepository>();
            IList <Post>             posts         = new List <Post>();

            for (int i = 0; i < expectedAmountPosts; i++)
            {
                posts.Add(GenerateFake.Post());
            }
            var dataContext = new Mock <DataContext>();

            dataContext.Setup(x => x.Posts).ReturnsDbSet(posts);
            var postRepository = new PostRepository(dataContext.Object, logger);

            //Act
            var postsFromRepo = await postRepository.GetPosts();

            //Assert
            Assert.Equal(expectedAmountPosts, postsFromRepo.Count());
        }
예제 #4
0
        public async void GetPostById_PostExists_ReturnsCorrectPostId(int expectedId)
        {
            // Arrange
            IList <Post> posts = new List <Post> {
                new Post()
                {
                    Id      = expectedId,
                    Text    = $"This post should have the Id {expectedId}.",
                    Created = DateTime.Now
                },
                GenerateFake.Post(),
                GenerateFake.Post()
            };

            _mockContext.Setup(x => x.Posts).ReturnsDbSet(posts);

            // Act
            var post = await _mockRepo.GetPostById(expectedId);

            // Assert
            Assert.Equal(expectedId, post.Id);
        }