コード例 #1
0
 public BlogEntriesController(ILog logger, IBlogEntryBL blogEntryBL, IBlogEntryTagBL blogEntryTagBL, IBlogEntryCommentBL blogEntryCommentBL)
     : base(logger)
 {
     _logger             = logger;
     _blogEntryBL        = blogEntryBL;
     _blogEntryTagBL     = blogEntryTagBL;
     _blogEntryCommentBL = blogEntryCommentBL;
 }
コード例 #2
0
        public void SetUp()
        {
            _fixture = new Fixture();
            var mockEntityBL           = new Mock <IBlogEntryBL>();
            var mockBlogEntryTagBL     = new Mock <IBlogEntryTagBL>();
            var mockBlogEntryCommentBL = new Mock <IBlogEntryCommentBL>();
            var mockLog = new Mock <ILog>();

            _entities = _fixture.Build <BlogEntry>().With(x => x.State, 1).CreateMany(100).ToList();

            if (_entities.All(x => x.Id != 1))
            {
                _entities.Add(_fixture.Build <BlogEntry>().With(x => x.Id, 1)
                              .With(x => x.Header, "test url blog")
                              .With(x => x.HeaderUrl, "test-url-blog").Create());
            }

            List <FilterOption> filters = new List <FilterOption>();

            List <string> selectColumnsList = new List <string>
            {
                "Id",
                "Header",
                "Author",
                "CreationDate",
                "State",
                "StateName",
                "Tags",
                "TotalComments"
            };

            int page     = 1;
            int pageSize = 10;

            var entitiesView   = _fixture.Build <BlogEntryView>().With(x => x.State, 1).CreateMany(100).ToList();
            var paginationList = entitiesView.Skip((page - 1) * pageSize).Take(pageSize).ToList();

            mockEntityBL.Setup(mr => mr.GetAllView(filters, page, pageSize, "id", "asc", selectColumnsList))
            .Returns(paginationList);

            mockEntityBL.Setup(mr => mr.CountGetAllView(filters, page, pageSize))
            .Returns(paginationList.Count);

            mockEntityBL.Setup(mr => mr.GetAll()).Returns(_entities);
            mockEntityBL.Setup(mr => mr.Get(It.IsAny <int>()))
            .Returns((int id) => { return(_entities.FirstOrDefault(p => p.Id == id)); });

            mockEntityBL.Setup(mr => mr.GetByHeaderUrl(It.IsAny <string>()))
            .Returns((string headerUrl) => { return(_entities.FirstOrDefault(p => p.HeaderUrl == headerUrl)); });

            mockEntityBL.Setup(r => r.Insert(It.IsAny <BlogEntry>()))
            .Callback <BlogEntry>(newEntity =>
            {
                int maxEntityId            = _entities.Max(x => x.Id);
                int nextEntityId           = maxEntityId + 1;
                newEntity.Id               = nextEntityId;
                newEntity.CreationDate     = DateTime.Now;
                newEntity.LastActivityDate = DateTime.Now;
                _entities.Add(newEntity);
            });

            mockEntityBL.Setup(r => r.Update(It.IsAny <BlogEntry>()))
            .Callback <BlogEntry>(x =>
            {
                x.LastActivityDate = DateTime.Now;
            });

            List <Tag> tags = new List <Tag>
            {
                new Tag {
                    Id = 1, Name = "C#"
                },
                new Tag {
                    Id = 2, Name = "VB"
                }
            };

            mockBlogEntryTagBL.Setup(mr => mr.GetByIdBlogEntry(It.IsAny <int>()))
            .Returns((int id) => tags);

            List <BlogEntryComment> blogEntryComments = new List <BlogEntryComment>
            {
                new BlogEntryComment {
                    Id = 1, Name = "anonymous", Comment = "good"
                },
                new BlogEntryComment {
                    Id = 2, Name = "anonymous", Comment = "good"
                }
            };

            mockBlogEntryCommentBL.Setup(mr => mr.GetByIdBlogEntry(It.IsAny <int>()))
            .Returns((int id) => blogEntryComments);

            _entityBL           = mockEntityBL.Object;
            _blogEntryTagBL     = mockBlogEntryTagBL.Object;
            _blogEntryCommentBL = mockBlogEntryCommentBL.Object;
            _logger             = mockLog.Object;

            Mapper.Initialize(cfg => { cfg.CreateMap <BlogEntryModel, BlogEntry>(); });
        }