public void ValueInCacheBypassesVirtualPathProviderForAllAvailableDisplayModesForContext() { // Arrange string cacheKey = null; string mobileCacheKey = null; SetupFileExists(VIEW_VIRTUAL); SetupFileExists(MOBILE_VIEW_VIRTUAL); _engine.MockCache .Setup(c => c.InsertViewLocation(It.IsAny <HttpContextBase>(), It.IsAny <string>(), VIEW_VIRTUAL)) .Callback <HttpContextBase, string, string>((httpContext, key, virtualPath) => { cacheKey = key; _engine.MockCache .Setup(c => c.GetViewLocation(It.IsAny <HttpContextBase>(), key)) .Returns(MOBILE_VIEW_VIRTUAL) .Verifiable(); }) .Verifiable(); _engine.MockCache .Setup(c => c.InsertViewLocation(It.IsAny <HttpContextBase>(), It.IsAny <string>(), MOBILE_VIEW_VIRTUAL)) .Callback <HttpContextBase, string, string>((httpContext, key, virtualPath) => { mobileCacheKey = key; _engine.MockCache .Setup(c => c.GetViewLocation(It.IsAny <HttpContextBase>(), key)) .Returns(MOBILE_VIEW_VIRTUAL) .Verifiable(); }) .Verifiable(); // Act _engine.FindView(_mobileContext, "name", null, false); _engine.FindView(_mobileContext, "name", null, true); // Assert // DefaultDisplayMode with Mobile substitution is cached and hit on the second call to FindView _engine.MockPathProvider.Verify(vpp => vpp.FileExists(MOBILE_VIEW_VIRTUAL), Times.AtMostOnce()); _engine.MockCache.Verify(c => c.InsertViewLocation(It.IsAny <HttpContextBase>(), It.IsAny <string>(), MOBILE_VIEW_VIRTUAL), Times.AtMostOnce()); _engine.MockCache.Verify(c => c.GetViewLocation(It.IsAny <HttpContextBase>(), VirtualPathProviderViewEngine.AppendDisplayModeToCacheKey(cacheKey, DisplayModeProvider.MobileDisplayModeId)), Times.AtMostOnce()); _engine.MockPathProvider.Verify(vpp => vpp.FileExists(VIEW_VIRTUAL), Times.AtMostOnce()); _engine.MockCache.Verify(c => c.InsertViewLocation(It.IsAny <HttpContextBase>(), It.IsAny <string>(), VIEW_VIRTUAL), Times.Exactly(1)); Assert.NotEqual(cacheKey, mobileCacheKey); // Act _engine.FindView(_context, "name", null, true); // Assert // The first call to FindView without a mobile browser results in a cache hit _engine.MockPathProvider.Verify(vpp => vpp.FileExists(VIEW_VIRTUAL), Times.AtMostOnce()); _engine.MockCache.Verify(c => c.InsertViewLocation(It.IsAny <HttpContextBase>(), It.IsAny <string>(), VIEW_VIRTUAL), Times.Exactly(1)); _engine.MockCache.Verify(c => c.GetViewLocation(It.IsAny <HttpContextBase>(), cacheKey), Times.Exactly(1)); }
public void ValueInCacheBypassesVirtualPathProvider() { // Arrange string cacheKey = null; SetupFileExists(VIEW_VIRTUAL); // It wasn't found, so they call vpp.FileExists SetupFileDoesNotExist(MOBILE_VIEW_VIRTUAL); SetupCacheMiss(CreateCacheKey(Cache.View, name: "name", displayMode: "Mobile")); _engine.MockCache // Then they set the value into the cache .Setup(c => c.InsertViewLocation(It.IsAny <HttpContextBase>(), It.IsAny <string>(), VIEW_VIRTUAL)) .Callback <HttpContextBase, string, string>((httpContext, key, virtualPath) => { cacheKey = key; _engine.MockCache // Second time through, we give them a cache hit .Setup(c => c.GetViewLocation(It.IsAny <HttpContextBase>(), key)) .Returns(VIEW_VIRTUAL) .Verifiable(); }) .Verifiable(); // Act _engine.FindView(_context, "name", null, false); // Call it once with false to seed the cache _engine.FindView(_context, "name", null, true); // Call it once with true to check the cache // Assert _engine.MockPathProvider.Verify(vpp => vpp.FileExists(VIEW_VIRTUAL), Times.AtMostOnce()); _engine.MockCache.Verify(c => c.InsertViewLocation(It.IsAny <HttpContextBase>(), It.IsAny <string>(), VIEW_VIRTUAL), Times.AtMostOnce()); _engine.MockCache.Verify(c => c.GetViewLocation(It.IsAny <HttpContextBase>(), cacheKey), Times.AtMostOnce()); // We seed the cache with all possible display modes but since the mobile view does not exist we don't insert it into the cache. _engine.MockPathProvider.Verify(vpp => vpp.FileExists(MOBILE_VIEW_VIRTUAL), Times.Exactly(1)); _engine.MockCache.Verify(c => c.InsertViewLocation(It.IsAny <HttpContextBase>(), It.IsAny <string>(), MOBILE_VIEW_VIRTUAL), Times.Never()); _engine.MockCache.Verify(c => c.GetViewLocation(It.IsAny <HttpContextBase>(), VirtualPathProviderViewEngine.AppendDisplayModeToCacheKey(cacheKey, DisplayModeProvider.MobileDisplayModeId)), Times.Never()); }