예제 #1
0
        public async Task GetHeadersPageTaggedNullDataTest()
        {
            //Arrange
            var mockCache = new Mock <ICacheAccessor>();
            var mockDb    = new Mock <IBlogAccessor>();

            mockDb.Setup(m => m.GetPostHeaderPageByTag(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>()))
            .ReturnsAsync(() => null);

            var engine = new BlogEngine(mockDb.Object, mockCache.Object);

            //Act
            var results = await engine.GetPageOfHeadersByTag(0, 2, TestConstants.GuidString);

            //Assert
            Assert.IsNull(results, "Should be null");

            mockCache.Verify(m =>
                             m.CacheEnt(It.IsAny <string>(), It.IsAny <IEnumerable <PostHeader> >(), It.IsAny <TimeSpan?>()),
                             Times.Never, "Should be skipped because of null");
        }
예제 #2
0
        public async Task GetHeadersPageTaggedCachedTest()
        {
            //Arrange
            var mockCache = new Mock <ICacheAccessor>();
            var mockDb    = new Mock <IBlogAccessor>();

            mockCache.Setup(m => m.GetEnt <IEnumerable <PostHeader> >(It.IsAny <string>()))
            .ReturnsAsync(() => new[]
            {
                new PostHeader
                {
                    BodyId = 1,
                    Id     = 1,
                    Title  = TestConstants.GuidString,
                    Tags   = new []
                    {
                        new Tag
                        {
                            Id   = 1,
                            Text = TestConstants.GuidString
                        },
                        new Tag
                        {
                            Id   = 2,
                            Text = TestConstants.GuidString
                        }
                    }
                },
                new PostHeader
                {
                    BodyId = 2,
                    Id     = 2,
                    Title  = TestConstants.GuidString,
                    Tags   = new []
                    {
                        new Tag
                        {
                            Id   = 3,
                            Text = TestConstants.GuidString
                        },
                        new Tag
                        {
                            Id   = 4,
                            Text = TestConstants.GuidString
                        }
                    }
                },
            });

            var engine = new BlogEngine(mockDb.Object, mockCache.Object);

            //Act
            var results = await engine.GetPageOfHeadersByTag(0, 2, TestConstants.GuidString);

            //Assert
            Assert.IsNotNull(results, "Should never be null");
            Assert.IsInstanceOfType(results, typeof(IEnumerable <PostHeader>));
            Assert.AreEqual(2, results.Count(), "Should be two headers on this page");

            mockDb.Verify(m => m.GetPostHeaderPageByTag(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>()),
                          Times.Never, "Should never reach db because results were cached.");

            mockCache.Verify(m =>
                             m.CacheEnt <IEnumerable <PostHeader> >(It.IsAny <string>(), It.IsAny <IEnumerable <PostHeader> >(), It.IsAny <TimeSpan?>()),
                             Times.Never, "Should have bailed before getting here");
        }