Exemple #1
0
        public void GenericMethodTest()
        {
            Type       type           = this.GetType();
            MethodInfo destMethodInfo = type.GetMethod("GenericMethodToBeReplaced", BindingFlags.NonPublic | BindingFlags.Instance);
            MethodInfo srcMethodInfo  = type.GetMethod("GenericMethodSourceILCodeToBeCopiedFrom", BindingFlags.NonPublic | BindingFlags.Instance);

            byte[] ilCodes = srcMethodInfo.GetMethodBody().GetILAsByteArray();

            Console.WriteLine(string.Format(@"Generic methods are most complicated, see the article for details.

{0}
-----------------------------

"
                                            , GenericMethodToBeReplaced <string, int>("11", 2)
                                            ));


            MockEngine.Mock(destMethodInfo, ilCodes);

            Console.WriteLine(string.Format(@"After updating the IL Code which is copied from another generic method.

{0}

{1}
-----------------------------
"
                                            , GenericMethodToBeReplaced <string, int>("11", 2)
                                            , GenericMethodToBeReplaced <long, int>(1, 2)
                                            ));
        }
        public void StaticMethodTest()
        {
            var result = Test.GetLargerNumberStatic(1, 2);

            Assert.AreEqual(2, result);

            var methodInfo = typeof(Test).GetMethod("GetLargerNumberStatic", BindingFlags.Public | BindingFlags.Static);
            var mock       = typeof(Tests).GetMethod("GetLargerNumberStatic", BindingFlags.Public | BindingFlags.Static);

            MockEngine.Mock(methodInfo, mock);

            var resultAfter = Test.GetLargerNumberStatic(1, 2);

            Assert.AreEqual(1, resultAfter);
        }
        public void PrivateMethodTest()
        {
            var test   = new Test();
            var result = test.GetLargerNumberPrivateProxy(1, 2);

            Assert.AreEqual(2, result);

            var methodInfo = typeof(Test).GetMethod("GetLargerNumberPrivte", BindingFlags.NonPublic | BindingFlags.Instance);

            MockEngine.Mock(methodInfo, act);

            var testAfter   = new Test();
            var resultAfter = testAfter.GetLargerNumberPrivateProxy(1, 2);

            Assert.AreEqual(1, resultAfter);
        }
        public void VirtualMethodTest()
        {
            var test   = new Test();
            var result = test.GetLargerNumberVirtual(1, 2);

            Assert.AreEqual(2, result);

            var methodInfo = typeof(Test).GetMethod("GetLargerNumberVirtual");

            MockEngine.Mock(methodInfo, act);

            var testAfter   = new Test();
            var resultAfter = testAfter.GetLargerNumberVirtual(1, 2);

            Assert.AreEqual(1, resultAfter);
        }
        public void ConstructorTest()
        {
            var test = new Test();

            Assert.AreEqual(5, test.Number);

            Action func = () => { };

            var constructor = typeof(Test).GetConstructor(Type.EmptyTypes);

            MockEngine.Mock(constructor, func);

            var testAfter = new Test();

            Assert.AreEqual(0, testAfter.Number);
        }
Exemple #6
0
        public void PublicMethodTest()
        {
            var test   = new Test();
            var result = test.GetLargerNumber(1, 2);

            NUnit.Framework.Assert.AreEqual(2, result);

            var methodInfo = typeof(Test).GetMethod("GetLargerNumber");

            MockEngine.Mock(methodInfo, act);

            var testAfter   = new Test();
            var resultAfter = testAfter.GetLargerNumber(1, 2);

            NUnit.Framework.Assert.AreEqual(1, resultAfter);
        }
        public void ProperyGetTest()
        {
            var test = new Test();

            test.SetNumber(5);

            Assert.AreEqual(5, test.NextNumber);

            Func <int> func = () => 10;

            var methodInfo = typeof(Test).GetProperty("NextNumber").GetGetMethod();

            MockEngine.Mock(methodInfo, func);

            var testAfter = new Test();

            Assert.AreEqual(10, testAfter.NextNumber);
        }
        public void OverridedMethodTest()
        {
            var test   = new ConcreteTest();
            var result = test.GetLargerNumberVirtual(1, 2);

            Assert.AreEqual(2, result);

            var methodInfo = typeof(ConcreteTest).GetMethod("GetLargerNumberVirtual");

            MockEngine.Mock(methodInfo, act);

            var testAfter   = new Test();
            var resultAfter = testAfter.GetLargerNumberVirtual(1, 2);

            var concreteTest   = new ConcreteTest();
            var concreteResult = concreteTest.GetLargerNumberVirtual(1, 2);

            Assert.AreEqual(2, resultAfter);
            Assert.AreEqual(1, concreteResult);
        }