public void InvokeICacheProvider_AddMethodOnceWithCorrectParameters() { // Arrange var cacheProvider = new Mock <ICacheProvider>(); var dateTimeProvider = new Mock <IDateTimeProvider>(); var cachingInterceptor = new CachingInterceptor(cacheProvider.Object, dateTimeProvider.Object); var invocation = new Mock <IInvocation>(); var invokedMethodName = "any string"; invocation.SetupGet(i => i.Request.Method.Name).Returns(invokedMethodName); var methodReturnValue = new Object(); invocation.SetupGet(p => p.ReturnValue).Returns(methodReturnValue); var dateTimeProviderUtcNow = DateTime.Parse("00:00:00.000"); dateTimeProvider.Setup(p => p.GetUtcNow()).Returns(dateTimeProviderUtcNow); var expectedAbsoluteExpirationParameterValue = dateTimeProviderUtcNow.AddMinutes(1); // Act cachingInterceptor.Intercept(invocation.Object); // Assert cacheProvider.Verify(p => p.Add(invokedMethodName, methodReturnValue, expectedAbsoluteExpirationParameterValue), Times.Once); }
public void TestIntercept_ShouldCallCachingProviderGetItem(string methodName) { // Arrange var mockedCachingProvider = new Mock <ICachingProvider>(); var mockedDateTimeProvider = new Mock <IDateTimeProvider>(); var interceptor = new CachingInterceptor(mockedCachingProvider.Object, mockedDateTimeProvider.Object); var mockedMethodInfo = new Mock <MethodInfo>(); mockedMethodInfo.Setup(m => m.Name).Returns(methodName); var mockedRequest = new Mock <IProxyRequest>(); mockedRequest.Setup(r => r.Method).Returns(mockedMethodInfo.Object); var mockedInvocation = new Mock <IInvocation>(); mockedInvocation.Setup(i => i.Request).Returns(mockedRequest.Object); // Act interceptor.Intercept(mockedInvocation.Object); // Assert mockedCachingProvider.Verify(p => p.GetItem(methodName), Times.Once); }
public void TestIntercept_CachingProviderReturnsResult_ShouldSetInvocationReturnValue() { // Arrange var obj = new object(); var mockedCachingProvider = new Mock <ICachingProvider>(); mockedCachingProvider.Setup(p => p.GetItem(It.IsAny <string>())).Returns(obj); var mockedDateTimeProvider = new Mock <IDateTimeProvider>(); var interceptor = new CachingInterceptor(mockedCachingProvider.Object, mockedDateTimeProvider.Object); var mockedMethodInfo = new Mock <MethodInfo>(); var mockedRequest = new Mock <IProxyRequest>(); mockedRequest.Setup(r => r.Method).Returns(mockedMethodInfo.Object); var mockedInvocation = new Mock <IInvocation>(); mockedInvocation.Setup(i => i.Request).Returns(mockedRequest.Object); // Act interceptor.Intercept(mockedInvocation.Object); // Assert mockedInvocation.VerifySet(i => i.ReturnValue = obj, Times.Once); }
public void TestIntercept_CachingProviderDoesNotReturnResult_ShouldCallDateTimeProviderGetTimeFromCurrentTime() { // Arrange var mockedCachingProvider = new Mock <ICachingProvider>(); var mockedDateTimeProvider = new Mock <IDateTimeProvider>(); var interceptor = new CachingInterceptor(mockedCachingProvider.Object, mockedDateTimeProvider.Object); var mockedMethodInfo = new Mock <MethodInfo>(); var mockedRequest = new Mock <IProxyRequest>(); mockedRequest.Setup(r => r.Method).Returns(mockedMethodInfo.Object); var mockedInvocation = new Mock <IInvocation>(); mockedInvocation.Setup(i => i.Request).Returns(mockedRequest.Object); // Act interceptor.Intercept(mockedInvocation.Object); // Assert mockedDateTimeProvider.Verify(p => p.GetTimeFromCurrentTime(Constants.HoursCaching, Constants.MinutesCaching, Constants.SecondsCaching), Times.Once); }
public void Initialize() { Cache = A.Fake <ObjectCache>(); CacheKeyGenerationService = A.Fake <ICacheKeyGenerationService>(); Invocation = A.Fake <IInvocation>(); Interceptor = new CachingInterceptor(Cache, CacheKeyGenerationService); }
public void TestConstructor_ShouldBeInstanceOfIInterceptor() { // Arrange var mockedCachingProvider = new Mock <ICachingProvider>(); var mockedDateTimeProvider = new Mock <IDateTimeProvider>(); // Act var interceptor = new CachingInterceptor(mockedCachingProvider.Object, mockedDateTimeProvider.Object); // Assert Assert.IsInstanceOf <IInterceptor>(interceptor); }
public void TestConstructor_PassEverything_ShouldInitializeCorrectly() { // Arrange var mockedCachingProvider = new Mock <ICachingProvider>(); var mockedDateTimeProvider = new Mock <IDateTimeProvider>(); // Act var interceptor = new CachingInterceptor(mockedCachingProvider.Object, mockedDateTimeProvider.Object); // Assert Assert.IsNotNull(interceptor); }
public void CreateCorrectIInterceptorInstance_WhenParametersAreValid() { // Arrange var cacheProvider = new Mock <ICacheProvider>(); var dateTimeProvider = new Mock <IDateTimeProvider>(); // Act var actualInstance = new CachingInterceptor(cacheProvider.Object, dateTimeProvider.Object); // Assert Assert.That(actualInstance, Is.Not.Null); Assert.That(actualInstance, Is.InstanceOf <IInterceptor>()); }
public void InvokeIInvocation_ProceedMethod_WhenCachedDataIsNotAvailable() { // Arrange var cacheProvider = new Mock <ICacheProvider>(); var dateTimeProvider = new Mock <IDateTimeProvider>(); var cachingInterceptor = new CachingInterceptor(cacheProvider.Object, dateTimeProvider.Object); var invokedMethodName = "any string"; var invocation = new Mock <IInvocation>(); invocation.SetupGet(i => i.Request.Method.Name).Returns(invokedMethodName); // Act cachingInterceptor.Intercept(invocation.Object); // Assert invocation.Verify(i => i.Proceed(), Times.Once); }
public void InvokeICacheProvider_IndexerGetMethodOnceWithCorrectParameter() { // Arrange var cacheProvider = new Mock <ICacheProvider>(); var dateTimeProvider = new Mock <IDateTimeProvider>(); var cachingInterceptor = new CachingInterceptor(cacheProvider.Object, dateTimeProvider.Object); var invokedMethodName = "any string"; var invocation = new Mock <IInvocation>(); invocation.SetupGet(i => i.Request.Method.Name).Returns(invokedMethodName); // Act cachingInterceptor.Intercept(invocation.Object); // Assert cacheProvider.Verify(p => p[invokedMethodName], Times.Once); }
public void InvokeIInvocation_RequestMethodNamePropertyGetMethod() { // Arrange var cacheProvider = new Mock <ICacheProvider>(); var dateTimeProvider = new Mock <IDateTimeProvider>(); var cachingInterceptor = new CachingInterceptor(cacheProvider.Object, dateTimeProvider.Object); var invokedMethodName = "any string"; var invocation = new Mock <IInvocation>(); invocation.SetupGet(i => i.Request.Method.Name).Returns(invokedMethodName); // Act cachingInterceptor.Intercept(invocation.Object); // Assert invocation.VerifyGet(i => i.Request.Method.Name, Times.Once); }
public void AssignCorrectValueToIInvocationReturnValueProperty_WhenCachedDataIsAvailable() { // Arrange var cacheProvider = new Mock <ICacheProvider>(); var dateTimeProvider = new Mock <IDateTimeProvider>(); var cachingInterceptor = new CachingInterceptor(cacheProvider.Object, dateTimeProvider.Object); var invokedMethodName = "any string"; var invocation = new Mock <IInvocation>(); invocation.SetupGet(i => i.Request.Method.Name).Returns(invokedMethodName); var cachedData = new Object(); cacheProvider.SetupGet(p => p[It.IsAny <string>()]).Returns(cachedData); // Act cachingInterceptor.Intercept(invocation.Object); // Assert invocation.VerifySet(i => i.ReturnValue = cachedData, Times.Once); }
public void TestIntercept_ShouldCallInvocationRequestArguments() { // Arrange var mockedCachingProvider = new Mock <ICachingProvider>(); var mockedDateTimeProvider = new Mock <IDateTimeProvider>(); var interceptor = new CachingInterceptor(mockedCachingProvider.Object, mockedDateTimeProvider.Object); var mockedMethodInfo = new Mock <MethodInfo>(); var mockedRequest = new Mock <IProxyRequest>(); mockedRequest.Setup(r => r.Method).Returns(mockedMethodInfo.Object); var mockedInvocation = new Mock <IInvocation>(); mockedInvocation.Setup(i => i.Request).Returns(mockedRequest.Object); // Act interceptor.Intercept(mockedInvocation.Object); // Assert mockedRequest.Verify(m => m.Arguments, Times.Once); }
public void TestIntercept_CachingProviderDoesNotReturnResult_ShouldCallCachingProviderAddItem(string methodName) { // Arrange var mockedCachingProvider = new Mock <ICachingProvider>(); var date = new DateTime(); var mockedDateTimeProvider = new Mock <IDateTimeProvider>(); mockedDateTimeProvider.Setup( p => p.GetTimeFromCurrentTime(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <int>())) .Returns(date); var interceptor = new CachingInterceptor(mockedCachingProvider.Object, mockedDateTimeProvider.Object); var mockedMethodInfo = new Mock <MethodInfo>(); mockedMethodInfo.Setup(m => m.Name).Returns(methodName); var mockedRequest = new Mock <IProxyRequest>(); mockedRequest.Setup(r => r.Method).Returns(mockedMethodInfo.Object); var returnValue = new object(); var mockedInvocation = new Mock <IInvocation>(); mockedInvocation.Setup(i => i.Request).Returns(mockedRequest.Object); mockedInvocation.Setup(i => i.ReturnValue).Returns(returnValue); // Act interceptor.Intercept(mockedInvocation.Object); // Assert mockedCachingProvider.Verify(p => p.AddItem(methodName, returnValue, date), Times.Once); }