public void ShouldThrowExceptionWhenGetCommentsByPostFails() { _commentsRepository = new Mock<ICommentRepository>(); _commentsRepository.Setup(a => a.Find(It.IsAny<Expression<Func<Comment, bool>>>(), It.IsAny<Func<IQueryable<Comment>, IOrderedQueryable<Comment>>>(), It.IsAny<string>())) .Throws(new Exception()); _userRepository = new Mock<IUserRepository>(); _commentsLogic = new CommentsLogic(_commentsRepository.Object, _userRepository.Object); Assert.Throws<BlogException>(() => _commentsLogic.GetByPostId(1)); }
public void ShouldGetCommentsByPost() { var expected = _comments.Where(a => a.PostId == 1).ToList(); _commentsRepository = new Mock<ICommentRepository>(); _commentsRepository.Setup(a => a.Find(It.IsAny<Expression<Func<Comment, bool>>>(), It.IsAny<Func<IQueryable<Comment>, IOrderedQueryable<Comment>>>(), It.IsAny<string>())) .Returns(expected); _userRepository = new Mock<IUserRepository>(); _commentsLogic = new CommentsLogic(_commentsRepository.Object, _userRepository.Object); var results = _commentsLogic.GetByPostId(1); Assert.NotNull(results); Assert.AreEqual(3, results.Count); Assert.AreEqual(1, results[0].PostId); Assert.AreEqual(1, results[1].PostId); Assert.AreEqual(1, results[2].PostId); }