コード例 #1
0
        public void Test_DefaultLogger_Simple_Shim()
        {
            using (ShimsContext.Create())
            {
                // Arrange
                var shim = new ShimSealedLoggerDependency();
                shim.DefaultLoggerGet = () => "DefaultLogger";
                var loggerDependency = shim.Instance;

                // Act
                string defaultLogger = loggerDependency.DefaultLogger;
                Console.WriteLine("Default logger is '{0}'", defaultLogger);

                // Assert
                Assert.That(defaultLogger, Is.EqualTo("DefaultLogger"));
            }
        }
コード例 #2
0
        public void Test_GetDirectoryByLoggerName_Shim_Always_Returns_The_Same_Value(string loggerName)
        {
            using (ShimsContext.Create())
            {
                // Arrange
                var shim = new ShimSealedLoggerDependency();
                // For any parameter in the GetDirectoryByLoggerName Stub should return "C:\\Foo".
                shim.GetDirectoryByLoggerNameString = (string logger) => "C:\\Foo";
                var loggerDependency = shim.Instance;

                // Act
                string directory = loggerDependency.GetDirectoryByLoggerName(loggerName);
                Console.WriteLine("Directory for the logger '{0}' is '{1}'", loggerName, directory);

                // Assert
                Assert.That(directory, Is.EqualTo("C:\\Foo"));
            }
        }
コード例 #3
0
        public void Test_GetCurrentDirrectory_Simple_Shim()
        {
            using (ShimsContext.Create())
            {
                // Arrange
                var shim = new ShimSealedLoggerDependency();
                shim.GetCurrentDirectory = () => "D:\\Temp";

                // Act
                // There is an explicit conversion from the shim to shimed object
                SealedLoggerDependency loggerDependency = shim;
                var currentDirectory = loggerDependency.GetCurrentDirectory();
                Console.WriteLine("Current directory is {0}", currentDirectory);

                // Assert
                Assert.That(currentDirectory, Is.EqualTo("D:\\Temp"));
            }
        }
コード例 #4
0
        public void Test_GetDirectoryByLoggerName_Shim_Returns_Different_Value_Based_On_The_Arguments(string loggerName)
        {
            using (ShimsContext.Create())
            {
                // Arrange
                var shim = new ShimSealedLoggerDependency();
                // Setting up our stub to return different values based on the argument.
                // This code is similar to following implementation:
                // public string GetDirectoryByLoggername(string s) { return "C:\\" + s; }
                shim.GetDirectoryByLoggerNameString = (string logger) => "C:\\" + logger;
                var loggerDependency = shim.Instance;

                string directory = loggerDependency.GetDirectoryByLoggerName(loggerName);

                Console.WriteLine("Directory for the logger '{0}' is '{1}'", loggerName, directory);

                // Assert
                Assert.That(directory, Is.EqualTo("C:\\" + loggerName));
            }
        }
コード例 #5
0
        public void Test_Shim_For_Generic_GetConfigValue_Of_T()
        {
            using (ShimsContext.Create())
            {
                // Arrange
                var shim = new ShimSealedLoggerDependency();
                shim.GetConfigValueOf1(() => "default");

                // Act
                var loggerDependency = shim.Instance;
                var str = loggerDependency.GetConfigValue<string>();
                Console.WriteLine("Config value is '{0}'", str);

                // Assert
                Assert.That(str, Is.EqualTo("default"));
            }
        }
コード例 #6
0
        public void Test_RollingRequired_Event_Simple_Shim()
        {
            using (ShimsContext.Create())
            {
                // Arrange
                var shim = new ShimSealedLoggerDependency();
                var loggerDependency = shim.Instance;

                // Act
                // We can't override firing event directly!
                // But we can override subscription and unsubscription.
                EventHandler backingDelegate = null;
                shim.RollingRequiredAddEventHandler = handler => backingDelegate += handler;

                bool eventOccurred = false;
                loggerDependency.RollingRequired += (sender, args) => eventOccurred = true;

                // Raising event manually
                backingDelegate(this, EventArgs.Empty);

                Assert.IsTrue(eventOccurred, "Event should be raised!");
            }
        }