public void OnResultExecuting_ItemsFlagSet_ReplacesOutputWriter() { // Arrange TextWriter originalWriter = TextWriter.Null; TextWriter newWriter = null; Mock <ResultExecutingContext> mockFilterContext = new Mock <ResultExecutingContext>(); mockFilterContext.Expect(o => o.HttpContext.Items[It.IsAny <object>()]).Returns("theCacheKey"); mockFilterContext.Expect(o => o.HttpContext.Response.Output).Returns(originalWriter); mockFilterContext .ExpectSet(o => o.HttpContext.Response.Output) .Callback(w => { newWriter = w; }); // Act ChildActionCacheAttribute attr = new ChildActionCacheAttribute() { Duration = 10 }; // Act attr.OnResultExecuting(mockFilterContext.Object); // Assert ChildActionCacheAttribute.WrappedStringWriter stringWriter = newWriter as ChildActionCacheAttribute.WrappedStringWriter; Assert.IsNotNull(stringWriter); Assert.AreEqual(originalWriter, stringWriter.OriginalWriter); }
public void OnActionExecuting_NotChildAction_DoesNothing() { // Arrange Mock <ActionExecutingContext> mockFilterContext = new Mock <ActionExecutingContext>(); mockFilterContext.Expect(o => o.IsChildAction).Returns(false).Verifiable(); ChildActionCacheAttribute attr = new ChildActionCacheAttribute() { Duration = 10 }; // Act attr.OnActionExecuting(mockFilterContext.Object); // Assert mockFilterContext.Verify(); }
public void OnActionExecuting_NegativeDuration_Throws() { // Arrange Mock <ActionExecutingContext> mockFilterContext = new Mock <ActionExecutingContext>(); mockFilterContext.Expect(o => o.IsChildAction).Returns(true); ChildActionCacheAttribute attr = new ChildActionCacheAttribute() { Duration = -1 }; // Act & assert ExceptionHelper.ExpectInvalidOperationException( delegate { attr.OnActionExecuting(mockFilterContext.Object); }, @"The 'Duration' property must be a positive number."); }
public void OnResultExecuting_ItemsFlagNotSet_DoesNothing() { // Arrange Mock <ResultExecutingContext> mockFilterContext = new Mock <ResultExecutingContext>(); mockFilterContext.Expect(o => o.HttpContext.Items).Returns(new Hashtable()).Verifiable(); // empty hashtable has no keys set mockFilterContext.Expect(o => o.HttpContext.Response.Output).Never(); // Act ChildActionCacheAttribute attr = new ChildActionCacheAttribute() { Duration = 10 }; // Act attr.OnResultExecuting(mockFilterContext.Object); // Assert mockFilterContext.Verify(); }
public void OnActionExecuting_NotChildAction_DoesNothing() { // Arrange Mock<ActionExecutingContext> mockFilterContext = new Mock<ActionExecutingContext>(); mockFilterContext.Expect(o => o.IsChildAction).Returns(false).Verifiable(); ChildActionCacheAttribute attr = new ChildActionCacheAttribute() { Duration = 10 }; // Act attr.OnActionExecuting(mockFilterContext.Object); // Assert mockFilterContext.Verify(); }
public void OnActionExecuting_NegativeDuration_Throws() { // Arrange Mock<ActionExecutingContext> mockFilterContext = new Mock<ActionExecutingContext>(); mockFilterContext.Expect(o => o.IsChildAction).Returns(true); ChildActionCacheAttribute attr = new ChildActionCacheAttribute() { Duration = -1 }; // Act & assert ExceptionHelper.ExpectInvalidOperationException( delegate { attr.OnActionExecuting(mockFilterContext.Object); }, @"The 'Duration' property must be a positive number."); }
public void OnResultExecuting_ItemsFlagSet_ReplacesOutputWriter() { // Arrange TextWriter originalWriter = TextWriter.Null; TextWriter newWriter = null; Mock<ResultExecutingContext> mockFilterContext = new Mock<ResultExecutingContext>(); mockFilterContext.Expect(o => o.HttpContext.Items[It.IsAny<object>()]).Returns("theCacheKey"); mockFilterContext.Expect(o => o.HttpContext.Response.Output).Returns(originalWriter); mockFilterContext .ExpectSet(o => o.HttpContext.Response.Output) .Callback(w => { newWriter = w; }); // Act ChildActionCacheAttribute attr = new ChildActionCacheAttribute() { Duration = 10 }; // Act attr.OnResultExecuting(mockFilterContext.Object); // Assert ChildActionCacheAttribute.WrappedStringWriter stringWriter = newWriter as ChildActionCacheAttribute.WrappedStringWriter; Assert.IsNotNull(stringWriter); Assert.AreEqual(originalWriter, stringWriter.OriginalWriter); }
public void OnResultExecuting_ItemsFlagNotSet_DoesNothing() { // Arrange Mock<ResultExecutingContext> mockFilterContext = new Mock<ResultExecutingContext>(); mockFilterContext.Expect(o => o.HttpContext.Items).Returns(new Hashtable()).Verifiable(); // empty hashtable has no keys set mockFilterContext.Expect(o => o.HttpContext.Response.Output).Never(); // Act ChildActionCacheAttribute attr = new ChildActionCacheAttribute() { Duration = 10 }; // Act attr.OnResultExecuting(mockFilterContext.Object); // Assert mockFilterContext.Verify(); }