コード例 #1
0
        public void InterceptTestHelper()
        {
            // Arrange
            string methodname = "IntOverloaded";

            Type[]        argumentTypes            = new Type[] { typeof(string) };
            IInterfaceMap interfaceMap             = CreateIInterfaceMap();
            MatchingInterceptor <TestClass> target = new MatchingInterceptor <TestClass>(interfaceMap);

            Mock <IInvocation> mock = new Mock <IInvocation>();

            mock.SetupAllProperties();
            mock.Setup(mocked => mocked.Arguments)
            .Returns(new object[] { "parameter" });
            mock.Setup(mocked => mocked.Method)
            .Returns(typeof(TestClass)
                     .GetMethod(methodname, argumentTypes));
            IInvocation invocation = mock.Object;

            // Act
            target.Intercept(invocation);

            // Assert
            Verify.That(invocation.ReturnValue).IsEqualTo(TestClass.IntOverloadedStringReturn)
            .Now();
        }
コード例 #2
0
        public void CanGetMappedMethodTestWithWithouhtGenericArguments()
        {
            // Arrange
            Type[]        argTypes = new Type[] { typeof(double) };
            IInterfaceMap target   = CreateIInterfaceMap();

            target.Subject = new TestClass();
            const string name = "IntOverloaded";
            MappedMethod actual;

            // Act
            actual = target.GetMappedMethod <TestClass>(name, argTypes);
            int result = (int)actual(1D);

            // Assert
            Verify.That(result).IsEqualTo(TestClass.IntOverloadedDoubleReturn).Now();
        }
コード例 #3
0
        public void CanRemoveNonContained()
        {
            // Arrange
            IInterfaceMap  target  = CreateIInterfaceMap();
            IMethodMapping mapping = CreateIMethodMapping();

            mapping.Name = "test";
            mapping.GenericArgumentTypes = Type.EmptyTypes;
            mapping.ArgumentTypes        = Type.EmptyTypes;
            bool actual;

            // Act
            actual = target.Remove(mapping);

            // Assert
            Verify.That(actual).IsFalse()
            .Now();
        }
コード例 #4
0
        public void CanGetMappedMethodTestWithReturnType()
        {
            // Arrange
            IInterfaceMap target = CreateIInterfaceMap();

            target.Subject = new TestClass();
            string name = "IntOverloaded";

            Type[]             argTypes    = new Type[] { typeof(double) };
            Type[]             genericArgs = Type.EmptyTypes;
            MappedMethod <int> actual;

            // Act
            actual = target.GetMappedMethod <int, TestClass>(name, argTypes, genericArgs);

            // Assert
            Verify.That(actual(1.0)).IsEqualTo(TestClass.IntOverloadedDoubleReturn)
            .Now();
        }
コード例 #5
0
        public void CanGetMappedMethodTestWithoutReturnType()
        {
            // Arrange
            Type[]        argTypes    = new Type[] { typeof(float) };
            Type[]        genericArgs = new Type[] { typeof(float) };
            IInterfaceMap target      = CreateIInterfaceMap();

            target.Subject = new TestClass();
            const string name = "IntOverloaded";
            MappedMethod actual;

            // Act
            actual = target.GetMappedMethod <TestClass>(name, argTypes, genericArgs);

            // Assert
            int result = (int)actual(1F);

            Verify.That(result).IsEqualTo(TestClass.IntOverloadedGenericReturn).Now();
        }
コード例 #6
0
        public void CanAddWithReplaceSetToFalse()
        {
            // Arrange
            IInterfaceMap  target  = CreateIInterfaceMap();
            IMethodMapping mapping = CreateIMethodMapping();

            mapping.Name = "test";
            mapping.GenericArgumentTypes = new Type[] { typeof(string), typeof(object) };
            mapping.ArgumentTypes        = new Type[] { typeof(bool) };
            mapping.Subject = (arg, args) =>
            {
                Console.Out.WriteLine("Subject called");
                return(true);
            };
            bool replace = false;

            // Act
            target.Add(mapping, replace);

            // Assert
            Verify.That(target.AsEnumerable()).DoesContain(mapping)
            .Now();
        }
コード例 #7
0
        public void CanReplaceAMethodMapping()
        {
            // Arrange
            IInterfaceMap target = CreateIInterfaceMap();

            IMethodMapping mapping = CreateIMethodMapping();

            mapping.Name = "test";
            mapping.GenericArgumentTypes = new Type[] { typeof(string), typeof(object) };
            mapping.ArgumentTypes        = new Type[] { typeof(bool) };
            mapping.Subject = (arg, args) =>
            {
                Console.Out.WriteLine("Subject called");
                return(true);
            };
            IMethodMapping replacement = CreateIMethodMapping();

            replacement.Name = "test";
            replacement.GenericArgumentTypes = new Type[] { typeof(string), typeof(object) };
            replacement.ArgumentTypes        = new Type[] { typeof(bool) };
            replacement.Subject = (arg, args) =>
            {
                Console.Out.WriteLine("Subject replacement called");
                return(true);
            };

            // Act
            target.Add(mapping, false);
            target.Add(replacement, true);

            // Assert
            Verify.That(target.AsEnumerable()).DoesContain(replacement, "Should contain the replacement")
            .Now();
            Verify.That(target.AsEnumerable()).DoesNotContain(mapping, "Should not contain the original mapping")
            .Now();
        }
コード例 #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MatchingInterceptor&lt;T&gt;"/> class.
 /// </summary>
 /// <param name="proxy">The proxy.</param>
 public MatchingInterceptor(IInterfaceMap interfaceMap)
 {
     Contract.Requires(interfaceMap != null);
     Contract.Ensures(this.interfaceMap == interfaceMap);
     this.interfaceMap = interfaceMap;
 }