public void ReturnTheCorrectResult_WhenTheCacheHasExpired()
        {
            // Arrange
            var commandMock        = new Mock <ICommand>();
            var cachingServiceMock = new Mock <ICachingService>();

            IList <string> parameters = new List <string>()
            {
                "DeathStar",
                "2016-1-1",
                "2018-05-04",
                "Active"
            };
            string methodName = "Execute";
            string result     = "Result of command execution";

            commandMock.Setup(c => c.Execute(parameters)).Returns(result);

            cachingServiceMock.Setup(c => c.IsExpired).Returns(true);
            cachingServiceMock.Setup(c => c.ResetCache());
            cachingServiceMock.Setup(c => c.AddCacheValue(It.IsAny <string>(), methodName, result));

            ICommand cacheableCommand = new CacheableCommand(commandMock.Object, cachingServiceMock.Object);

            // Act
            string actualResult = cacheableCommand.Execute(parameters);

            // Assert
            Assert.AreEqual(result, actualResult);
        }
        public void CallGetCacheValueOnce_WhenTheCacheHasNotExpired()
        {
            // Arrange
            var commandMock        = new Mock <ICommand>();
            var cachingServiceMock = new Mock <ICachingService>();

            IList <string> parameters = new List <string>()
            {
                "DeathStar",
                "2016-1-1",
                "2018-05-04",
                "Active"
            };
            string methodName = "Execute";
            string result     = "Result of command execution";

            cachingServiceMock.Setup(c => c.IsExpired).Returns(false);
            cachingServiceMock.Setup(c => c.GetCacheValue(It.IsAny <string>(), methodName)).Returns(result);

            ICommand cacheableCommand = new CacheableCommand(commandMock.Object, cachingServiceMock.Object);

            // Act
            cacheableCommand.Execute(parameters);

            // Assert
            cachingServiceMock.Verify(c => c.GetCacheValue(It.IsAny <string>(), methodName), Times.Once());
        }
Пример #3
0
        public void ThrowsArgumentNullExceptionWhenParametersAreNull()
        {
            // Arrange
            var decoratedCommandMock = new Mock <ICommand>();
            var cachingServiceMock   = new Mock <ICachingService>();

            var command = new CacheableCommand(decoratedCommandMock.Object, cachingServiceMock.Object);

            // Act & Assert
            Assert.Throws <ArgumentNullException>(() => command.Execute(null));
        }
Пример #4
0
        public void ReturnAnInstanceOfCacheableCommand()
        {
            // Arrange
            var commandMock        = new Mock <ICommand>();
            var cachingServiceMock = new Mock <ICachingService>();

            // Act
            ICommand cacheableCommand = new CacheableCommand(commandMock.Object, cachingServiceMock.Object);

            // Assert
            Assert.IsInstanceOf <CacheableCommand>(cacheableCommand);
        }
Пример #5
0
        public void SetTheParametersCountPropertyCorrectly()
        {
            // Arrange
            int parametersCount    = 4;
            var commandMock        = new Mock <ICommand>();
            var cachingServiceMock = new Mock <ICachingService>();

            commandMock.Setup(c => c.ParameterCount).Returns(parametersCount);

            // Act
            ICommand cacheableCommand = new CacheableCommand(commandMock.Object, cachingServiceMock.Object);

            // Assert
            Assert.AreEqual(parametersCount, cacheableCommand.ParameterCount);
        }
Пример #6
0
        public void GetCachedValueWhenTheCacheIsNotExpired()
        {
            // Arrange
            var decoratedCommandMock = new Mock <ICommand>();
            var cachingServiceMock   = new Mock <ICachingService>();

            cachingServiceMock.SetupGet(x => x.IsExpired).Returns(false);
            cachingServiceMock.Setup(x => x.GetCacheValue(It.IsAny <string>(), It.IsAny <string>())).Returns("result");

            var command = new CacheableCommand(decoratedCommandMock.Object, cachingServiceMock.Object);

            // Act
            var result = command.Execute(new List <string>());

            // Assert
            cachingServiceMock.Verify(x => x.GetCacheValue(It.IsAny <string>(), It.IsAny <string>()), Times.Once);
            Assert.AreEqual("result", result);
        }