public void OnActionExecuting_CacheMiss_SetsFlagInItems() {
            // Arrange
            string expectedCacheKey = null;

            Mock<ActionExecutingContext> mockFilterContext = new Mock<ActionExecutingContext>();
            mockFilterContext.Expect(o => o.IsChildAction).Returns(true);
            mockFilterContext.Expect(o => o.ActionDescriptor.ActionName).Returns("theActionName");
            mockFilterContext.Expect(o => o.ActionDescriptor.ControllerDescriptor.ControllerName).Returns("theControllerName");
            mockFilterContext.Expect(o => o.HttpContext.Items).Returns(new Hashtable());
            mockFilterContext.Expect(o => o.HttpContext.Response.ContentEncoding).Returns(Encoding.UTF8);
            mockFilterContext.Expect(o => o.ActionParameters).Returns(new Dictionary<string, object> {
                { "forty-two", 42 },
                { "eighty-four", 84 }
            });

            Mock<ITestableCache> mockCache = new Mock<ITestableCache>();
            mockCache
                .Expect(o => o.GetCacheItem(It.IsAny<string>()))
                .Callback((string cacheKey) => { expectedCacheKey = cacheKey; })
                .Returns((object)null);

            ChildActionCacheAttribute attr = new TestableChildActionCacheAttribute(mockCache.Object) {
                Duration = 10
            };

            // Act
            ActionExecutingContext filterContext = mockFilterContext.Object;
            attr.OnActionExecuting(filterContext);

            // Assert
            Assert.IsNull(filterContext.Result, "Result shouldn't have been set if cache miss.");
            Assert.AreEqual(expectedCacheKey, filterContext.HttpContext.Items.Values.Cast<object>().Single(), "Flag was not correctly set in HttpContext.Items.");
        }
Example #2
0
        public void OnActionExecuting_CacheHit_ReturnsContentResult()
        {
            // Arrange
            Mock <ActionExecutingContext> mockFilterContext = new Mock <ActionExecutingContext>();

            mockFilterContext.Expect(o => o.IsChildAction).Returns(true);
            mockFilterContext.Expect(o => o.ActionDescriptor.ActionName).Returns("theActionName");
            mockFilterContext.Expect(o => o.ActionDescriptor.ControllerDescriptor.ControllerName).Returns("theControllerName");
            mockFilterContext.Expect(o => o.HttpContext.Items).Never();
            mockFilterContext.Expect(o => o.HttpContext.Response.ContentEncoding).Returns(Encoding.UTF8);
            mockFilterContext.Expect(o => o.ActionParameters).Returns(new Dictionary <string, object> {
                { "forty-two", 42 },
                { "eighty-four", 84 }
            });

            Mock <ITestableCache> mockCache = new Mock <ITestableCache>();

            mockCache.Expect(o => o.GetCacheItem(It.IsAny <string>())).Returns("some cached content");

            ChildActionCacheAttribute attr = new TestableChildActionCacheAttribute(mockCache.Object)
            {
                Duration = 10
            };

            // Act
            ActionExecutingContext filterContext = mockFilterContext.Object;

            attr.OnActionExecuting(filterContext);

            // Assert
            Assert.IsInstanceOfType(filterContext.Result, typeof(ContentResult));
            Assert.AreEqual("some cached content", ((ContentResult)filterContext.Result).Content);
        }
        public void OnActionExecuting_CacheHit_ReturnsContentResult() {
            // Arrange
            Mock<ActionExecutingContext> mockFilterContext = new Mock<ActionExecutingContext>();
            mockFilterContext.Expect(o => o.IsChildAction).Returns(true);
            mockFilterContext.Expect(o => o.ActionDescriptor.ActionName).Returns("theActionName");
            mockFilterContext.Expect(o => o.ActionDescriptor.ControllerDescriptor.ControllerName).Returns("theControllerName");
            mockFilterContext.Expect(o => o.HttpContext.Items).Never();
            mockFilterContext.Expect(o => o.HttpContext.Response.ContentEncoding).Returns(Encoding.UTF8);
            mockFilterContext.Expect(o => o.ActionParameters).Returns(new Dictionary<string, object> {
                { "forty-two", 42 },
                { "eighty-four", 84 }
            });

            Mock<ITestableCache> mockCache = new Mock<ITestableCache>();
            mockCache.Expect(o => o.GetCacheItem(It.IsAny<string>())).Returns("some cached content");

            ChildActionCacheAttribute attr = new TestableChildActionCacheAttribute(mockCache.Object) {
                Duration = 10
            };

            // Act
            ActionExecutingContext filterContext = mockFilterContext.Object;
            attr.OnActionExecuting(filterContext);

            // Assert
            Assert.IsInstanceOfType(filterContext.Result, typeof(ContentResult));
            Assert.AreEqual("some cached content", ((ContentResult)filterContext.Result).Content);
        }
Example #4
0
        public void OnActionExecuting_CacheMiss_SetsFlagInItems()
        {
            // Arrange
            string expectedCacheKey = null;

            Mock <ActionExecutingContext> mockFilterContext = new Mock <ActionExecutingContext>();

            mockFilterContext.Expect(o => o.IsChildAction).Returns(true);
            mockFilterContext.Expect(o => o.ActionDescriptor.ActionName).Returns("theActionName");
            mockFilterContext.Expect(o => o.ActionDescriptor.ControllerDescriptor.ControllerName).Returns("theControllerName");
            mockFilterContext.Expect(o => o.HttpContext.Items).Returns(new Hashtable());
            mockFilterContext.Expect(o => o.HttpContext.Response.ContentEncoding).Returns(Encoding.UTF8);
            mockFilterContext.Expect(o => o.ActionParameters).Returns(new Dictionary <string, object> {
                { "forty-two", 42 },
                { "eighty-four", 84 }
            });

            Mock <ITestableCache> mockCache = new Mock <ITestableCache>();

            mockCache
            .Expect(o => o.GetCacheItem(It.IsAny <string>()))
            .Callback((string cacheKey) => { expectedCacheKey = cacheKey; })
            .Returns((object)null);

            ChildActionCacheAttribute attr = new TestableChildActionCacheAttribute(mockCache.Object)
            {
                Duration = 10
            };

            // Act
            ActionExecutingContext filterContext = mockFilterContext.Object;

            attr.OnActionExecuting(filterContext);

            // Assert
            Assert.IsNull(filterContext.Result, "Result shouldn't have been set if cache miss.");
            Assert.AreEqual(expectedCacheKey, filterContext.HttpContext.Items.Values.Cast <object>().Single(), "Flag was not correctly set in HttpContext.Items.");
        }