예제 #1
0
        public void ShouldGetTagsByPostId()
        {
            const int postId = 1;
            var tags = _tags.Where(a => a.TagId != 3).ToList();
            var post = _posts.Where(a => a.PostId == 1).ToList();

            _tagRepository = new Mock<ITagRepository>();
            _tagRepository.Setup(a => a.Find(It.IsAny<Expression<Func<Tag, bool>>>(), null, string.Empty))
                .Returns(tags);

            _postRepository = new Mock<IPostRepository>();
            _postRepository.Setup(a => a.Find(It.IsAny<Expression<Func<Post, bool>>>(),
                It.IsAny<Func<IQueryable<Post>, IOrderedQueryable<Post>>>(), It.IsAny<string>()))
                .Returns(post);

            _tagsLogic = new TagsLogic(_tagRepository.Object, _postRepository.Object);

            var result = _tagsLogic.GetByPostId(postId);

            Assert.AreEqual(2, result.Count);

        }
예제 #2
0
        public void ShouldErrorWhenTagExistsOnAdd()
        {
            var tag = new Common.Contracts.Tag { TagName = "lorem" };
            var tags = _tags.Where(a => a.TagName == "lorem").ToList();

            _tagRepository = new Mock<ITagRepository>();
            _tagRepository.Setup(a => a.Add(It.IsAny<Tag>())).Returns(It.IsAny<Tag>);
            _tagRepository.Setup(a => a.Find(It.IsAny<Expression<Func<Tag, bool>>>(),
                It.IsAny<Func<IQueryable<Tag>, IOrderedQueryable<Tag>>>(), It.IsAny<string>()))
                .Returns(tags);

            _postRepository = new Mock<IPostRepository>();

            _tagsLogic = new TagsLogic(_tagRepository.Object, _postRepository.Object);

            var result = _tagsLogic.Add(tag);

            Assert.NotNull(result.Error);
            Assert.AreEqual((int)Constants.Error.ValidationError, result.Error.Id);
            Assert.AreEqual("Record already exists", result.Error.Message);
        }
예제 #3
0
        public void ShouldThrowExceptionWhenAddingTagFails()
        {
            var tag = new Common.Contracts.Tag { TagName = "foo" };
            _tagRepository = new Mock<ITagRepository>();
            _tagRepository.Setup(a => a.Find(It.IsAny<Expression<Func<Tag, bool>>>(), null, string.Empty))
                .Throws(new Exception());

            _postRepository = new Mock<IPostRepository>();

            _tagsLogic = new TagsLogic(_tagRepository.Object, _postRepository.Object);

            Assert.Throws<BlogException>(() => _tagsLogic.Add(tag));
        }
예제 #4
0
        public void ShouldAddTag()
        {
            var tag = new Common.Contracts.Tag {TagName = "foo"};
            _tagRepository = new Mock<ITagRepository>();
            _tagRepository.Setup(a => a.Add(It.IsAny<Tag>())).Returns(new Tag { TagName = "foo" });
            _tagRepository.Setup(a => a.Find(It.IsAny<Expression<Func<Tag, bool>>>(),
                It.IsAny<Func<IQueryable<Tag>, IOrderedQueryable<Tag>>>(), It.IsAny<string>()))
                .Returns(new List<Tag>());

            _postRepository = new Mock<IPostRepository>();

            _tagsLogic = new TagsLogic(_tagRepository.Object, _postRepository.Object);

            var result = _tagsLogic.Add(tag);

            Assert.NotNull(result);
            Assert.IsNull(result.Error);
        }
예제 #5
0
        public void ShouldThrowExceptionWhenGetTagsByNameFails()
        {
            _tagRepository = new Mock<ITagRepository>();
            _tagRepository.Setup(a => a.Find(It.IsAny<Expression<Func<Tag, bool>>>(), null, string.Empty))
                .Throws(new Exception());

            _postRepository = new Mock<IPostRepository>();

            _tagsLogic = new TagsLogic(_tagRepository.Object, _postRepository.Object);

            Assert.Throws<BlogException>(() => _tagsLogic.GetTagsByName("foo"));
        }
예제 #6
0
        public void ShouldGetTagsByName()
        {
            const string tagName = "ipsum";
            var tags = _tags.Where(a => a.TagName.Contains(tagName)).ToList();

            _tagRepository = new Mock<ITagRepository>();
            _tagRepository.Setup(a => a.Find(It.IsAny<Expression<Func<Tag, bool>>>(), true))
                .Returns(tags);

            _postRepository = new Mock<IPostRepository>();

            _tagsLogic = new TagsLogic(_tagRepository.Object, _postRepository.Object);

            var result = _tagsLogic.GetTagsByName(tagName);

            Assert.AreEqual(2, result.Count);
        }
예제 #7
0
        public void ShouldReturnEmptyListWhenGetTagsByPostIdFoundNoRecords()
        {
            _tagRepository = new Mock<ITagRepository>();
            _tagRepository.Setup(a => a.Find(It.IsAny<Expression<Func<Tag, bool>>>(), null, string.Empty))
                .Returns(new List<Tag>());

            _postRepository = new Mock<IPostRepository>();
            _postRepository.Setup(a => a.Find(It.IsAny<Expression<Func<Post, bool>>>(),
                It.IsAny<Func<IQueryable<Post>, IOrderedQueryable<Post>>>(), It.IsAny<string>()))
                .Returns(new List<Post>());

            _tagsLogic = new TagsLogic(_tagRepository.Object, _postRepository.Object);

            var tags = _tagsLogic.GetByPostId(1);

            Assert.AreEqual(0, tags.Count);
        }
예제 #8
0
        public void ShouldThrowExceptionWhenGetTagsByPostIdFails()
        {
            _tagRepository = new Mock<ITagRepository>();
            _tagRepository.Setup(a => a.Find(It.IsAny<Expression<Func<Tag, bool>>>(), null, string.Empty))
                .Throws(new Exception());

            _postRepository = new Mock<IPostRepository>();
            _postRepository.Setup(a => a.Find(It.IsAny<Expression<Func<Post, bool>>>(),
                It.IsAny<Func<IQueryable<Post>, IOrderedQueryable<Post>>>(), It.IsAny<string>()))
                .Throws(new Exception());

            _tagsLogic = new TagsLogic(_tagRepository.Object, _postRepository.Object);
            
            Assert.Throws<BlogException>(() => _tagsLogic.GetByPostId(1));
        }