public void ShouldValidateAssumptionAboutRealProxyObjectCastingTest()
        {
            MockDynamicInvokerRealProxy realProxy;
            IMockObject        transparentProxy;
            Mockery            mockery;
            IDynamicInvocation mockDynamicInvocation;
            IDisposable        disposable;
            MethodInfo         invokedMethodInfo;

            mockery = new Mockery();
            mockDynamicInvocation = mockery.NewMock <IDynamicInvocation>();

            invokedMethodInfo = (MethodInfo)MemberInfoProxy <IDisposable> .GetLastMemberInfo(exec => exec.Dispose());

            realProxy = new MockDynamicInvokerRealProxy(mockDynamicInvocation);
            Assert.IsNotNull(realProxy);

            Expect.Once.On(mockDynamicInvocation).Method("Invoke").With(new EqualMatcher(typeof(IMockObject)), new EqualMatcher(invokedMethodInfo), new EqualMatcher(realProxy.GetTransparentProxy()), new EqualMatcher(new object[] { })).Will(Return.Value(null));

            transparentProxy = (IMockObject)realProxy.GetTransparentProxy();
            Assert.IsNotNull(transparentProxy);
            Assert.IsTrue(RemotingServices.IsTransparentProxy((object)transparentProxy));
            Assert.IsTrue(RemotingServices.GetRealProxy((object)transparentProxy) == (object)realProxy);
            Assert.IsFalse((object)realProxy == (object)transparentProxy);

            Assert.IsTrue(realProxy.CanCastTo(typeof(IDisposable), null));
            Assert.IsFalse(realProxy.CanCastTo(typeof(IConvertible), null));
            Assert.IsTrue(transparentProxy is IDisposable);

            disposable = (IDisposable)transparentProxy;
            disposable.Dispose();

            mockery.VerifyAllExpectationsHaveBeenMet();
        }
Example #2
0
        public void ShouldCreateInstanceWithCacheKeyAndInvokeDynamicFactoryTest()
        {
            Mockery          mockery;
            MockProxyFactory factory;
            IMockObject      objectContract;

            MockProxyFactory.IInvokeDynamicFactory mockInvokeDynamicFactory;
            IDynamicInvocation mockDynamicInvocation;
            MethodInfo         invokedMethodInfo;

            mockery = new Mockery();
            mockDynamicInvocation    = mockery.NewMock <IDynamicInvocation>();
            mockInvokeDynamicFactory = mockery.NewMock <MockProxyFactory.IInvokeDynamicFactory>();

            invokedMethodInfo = (MethodInfo)MemberInfoProxy <IDisposable> .GetLastMemberInfo(exec => exec.Dispose());

            Expect.Once.On(mockInvokeDynamicFactory).Method("GetDynamicInvoker").With("myCacheKey", typeof(IMockObject)).Will(Return.Value(mockDynamicInvocation));

            factory = new MockProxyFactory();

            Assert.IsNotNull(factory);

            objectContract = factory.CreateInstance("myCacheKey", mockInvokeDynamicFactory);
            Assert.IsNotNull(objectContract);

            factory.Dispose();

            mockery.VerifyAllExpectationsHaveBeenMet();
        }
        public void ShouldInvokeAsObjectTest()
        {
            Mockery mockery;
            MockDynamicInvokerRealProxy mockDynamicInvokerRealProxy;
            IDynamicInvocation          mockDynamicInvocation;
            IMethodCallMessage          mockMessage;
            MethodInfo invokedMethodInfo;

            mockery = new Mockery();
            mockDynamicInvocation = mockery.NewMock <IDynamicInvocation>();
            mockMessage           = mockery.NewMock <IMethodCallMessage>();

            invokedMethodInfo = (MethodInfo)MemberInfoProxy <IMockObject> .GetLastMemberInfo(exec => exec.GetType());

            Expect.Once.On(mockMessage).GetProperty("Args").Will(Return.Value(new object[] { }));
            Expect.Exactly(2).On(mockMessage).GetProperty("MethodBase").Will(Return.Value(invokedMethodInfo));
            Expect.Once.On(mockMessage).GetProperty("LogicalCallContext").Will(Return.Value(null));
            Expect.Once.On(mockMessage).GetProperty("Uri").Will(Return.Value(null));
            Expect.Once.On(mockMessage).GetProperty("MethodName").Will(Return.Value(null));
            Expect.Once.On(mockMessage).GetProperty("TypeName").Will(Return.Value(null));
            Expect.Once.On(mockMessage).GetProperty("HasVarArgs").Will(Return.Value(false));

            mockDynamicInvokerRealProxy = new MockDynamicInvokerRealProxy(mockDynamicInvocation);

            Expect.Once.On(mockDynamicInvocation).Method("Invoke").With(new EqualMatcher(typeof(IMockObject)), new EqualMatcher(invokedMethodInfo), new EqualMatcher(mockDynamicInvokerRealProxy.GetTransparentProxy()), new EqualMatcher(new object[] { })).Will(Return.Value(null));

            Assert.IsNotNull(mockDynamicInvokerRealProxy);

            mockDynamicInvokerRealProxy.Invoke(mockMessage);

            mockery.VerifyAllExpectationsHaveBeenMet();
        }
Example #4
0
        public void ShouldEnsureCachingTest()
        {
            Mockery          mockery;
            MockProxyFactory factory;
            IMockObject      objectContract0, objectContract1, objectContract2, objectContract3;

            MockProxyFactory.IInvokeDynamicFactory mockInvokeDynamicFactory;
            IDynamicInvocation mockDynamicInvocation;
            MethodInfo         invokedMethodInfo;

            mockery = new Mockery();
            mockDynamicInvocation    = mockery.NewMock <IDynamicInvocation>();
            mockInvokeDynamicFactory = mockery.NewMock <MockProxyFactory.IInvokeDynamicFactory>();

            invokedMethodInfo = (MethodInfo)MemberInfoProxy <IDisposable> .GetLastMemberInfo(exec => exec.Dispose());

            Expect.Exactly(1).On(mockInvokeDynamicFactory).Method("GetDynamicInvoker").With("myCacheKey", typeof(IMockObject)).Will(Return.Value(mockDynamicInvocation));
            Expect.Exactly(1).On(mockInvokeDynamicFactory).Method("GetDynamicInvoker").With("myCacheKey_different", typeof(IMockObject)).Will(Return.Value(mockDynamicInvocation));

            factory = new MockProxyFactory();

            Assert.IsNotNull(factory);

            objectContract0 = factory.CreateInstance("myCacheKey", mockInvokeDynamicFactory);
            objectContract1 = factory.CreateInstance("myCacheKey_different", mockInvokeDynamicFactory);
            objectContract2 = factory.CreateInstance("myCacheKey", mockInvokeDynamicFactory);
            objectContract3 = factory.CreateInstance(mockDynamicInvocation);

            Assert.IsNotNull(objectContract0);
            Assert.IsNotNull(objectContract1);
            Assert.IsNotNull(objectContract2);
            Assert.IsNotNull(objectContract3);

            Assert.AreNotSame(objectContract0, objectContract1);
            Assert.AreSame(objectContract0, objectContract2);
            Assert.AreNotSame(objectContract1, objectContract2);

            Assert.AreNotSame(objectContract0, objectContract3);
            Assert.AreNotSame(objectContract1, objectContract3);
            Assert.AreNotSame(objectContract2, objectContract3);

            factory.Dispose();

            mockery.VerifyAllExpectationsHaveBeenMet();
        }
Example #5
0
        public void ShouldFailOnNullTargetInstanceInvokeTest()
        {
            MockDynamicInvoker mockDynamicInvoker;
            object             proxyInstance;
            Type       proxiedType;
            MethodInfo invokedMethodInfo;

            object[] invocationParameters;

            proxyInstance     = null;
            proxiedType       = typeof(IMockObject);
            invokedMethodInfo = (MethodInfo)MemberInfoProxy <IMockObject> .GetLastMemberInfo(exec => exec.ToString());

            invocationParameters = new object[] { };

            mockDynamicInvoker = new MockDynamicInvoker();
            mockDynamicInvoker.Invoke(proxiedType, invokedMethodInfo, proxyInstance, invocationParameters);
        }
Example #6
0
        public void ShouldFailOnNullProxyTypeInvokeOnObjectTest()
        {
            MockDynamicInvoker mockDynamicInvoker;
            object             proxyInstance;
            Type       proxiedType;
            MethodInfo invokedMethodInfo;

            object[] invocationParameters;

            proxyInstance     = new object();
            proxiedType       = null;
            invokedMethodInfo = (MethodInfo)MemberInfoProxy <IMockObject> .GetLastMemberInfo(exec => exec.GetType());

            invocationParameters = new object[] { };

            mockDynamicInvoker = new MockDynamicInvoker();
            mockDynamicInvoker.BypassInvokeOnObject(proxiedType, invokedMethodInfo, proxyInstance, invocationParameters);
        }
Example #7
0
        public void ShouldFailOnBadInputParameterCountToStringInvokeOnObjectTest()
        {
            MockDynamicInvoker mockDynamicInvoker;
            object             proxyInstance;
            Type       proxiedType;
            MethodInfo invokedMethodInfo;

            object[] invocationParameters;

            proxyInstance     = new object();
            proxiedType       = typeof(IMockObject);
            invokedMethodInfo = (MethodInfo)MemberInfoProxy <IMockObject> .GetLastMemberInfo(exec => exec.ToString());

            invocationParameters = new object[] { 0 };

            mockDynamicInvoker = new MockDynamicInvoker();
            mockDynamicInvoker.BypassInvokeOnObject(proxiedType, invokedMethodInfo, proxyInstance, invocationParameters);
        }
Example #8
0
        public void ShouldFailOnBadDeclaringTypeInvokeOnObjectTest()
        {
            MockDynamicInvoker mockDynamicInvoker;
            object             proxyInstance;
            Type       proxiedType;
            MethodInfo invokedMethodInfo;

            object[] invocationParameters;

            proxyInstance     = new object();
            proxiedType       = typeof(IDisposable);
            invokedMethodInfo = (MethodInfo)MemberInfoProxy <IDisposable> .GetLastMemberInfo(exec => exec.Dispose());

            invocationParameters = new object[] { };

            mockDynamicInvoker = new MockDynamicInvoker();
            mockDynamicInvoker.BypassInvokeOnObject(proxiedType, invokedMethodInfo, proxyInstance, invocationParameters);
        }
        public void ShouldThrowOnProceedInvokeTest()
        {
            MockAspectDynamicInvoker mockAspectDynamicInvoker;
            object     proxyInstance;
            Type       proxiedType;
            MethodInfo invokedMethodInfo;

            object[] invocationParameters;
            object   returnValue;

            proxyInstance     = new object();
            proxiedType       = typeof(IMockObject);
            invokedMethodInfo = (MethodInfo)MemberInfoProxy <IMockObject> .GetLastMemberInfo(exec => exec.ToString());

            invocationParameters = new object[] { };

            mockAspectDynamicInvoker = new MockAspectDynamicInvoker(new InvalidOperationException());
            returnValue = mockAspectDynamicInvoker.Invoke(proxiedType, invokedMethodInfo, proxyInstance, invocationParameters);
        }
Example #10
0
        public void ShouldEqualsInvokeOnObjectTest()
        {
            MockDynamicInvoker mockDynamicInvoker;
            object             proxyInstance;
            Type       proxiedType;
            MethodInfo invokedMethodInfo;

            object[] invocationParameters;
            object   returnValue;

            proxyInstance     = new object();
            proxiedType       = typeof(IMockObject);
            invokedMethodInfo = (MethodInfo)MemberInfoProxy <IMockObject> .GetLastMemberInfo(exec => exec.Equals(null));

            invocationParameters = new object[] { null };

            mockDynamicInvoker = new MockDynamicInvoker();
            returnValue        = mockDynamicInvoker.BypassInvokeOnObject(proxiedType, invokedMethodInfo, proxyInstance, invocationParameters);

            Assert.AreEqual(false, returnValue);
        }
Example #11
0
        public void ShouldToStringInvokeTest()
        {
            MockDynamicInvoker mockDynamicInvoker;
            object             proxyInstance;
            Type       proxiedType;
            MethodInfo invokedMethodInfo;

            object[] invocationParameters;
            object   returnValue;

            proxyInstance     = new object();
            proxiedType       = typeof(IMockObject);
            invokedMethodInfo = (MethodInfo)MemberInfoProxy <IMockObject> .GetLastMemberInfo(exec => exec.ToString());

            invocationParameters = new object[] { };

            mockDynamicInvoker = new MockDynamicInvoker();
            returnValue        = mockDynamicInvoker.Invoke(proxiedType, invokedMethodInfo, proxyInstance, invocationParameters);

            Assert.AreEqual(typeof(IMockObject).FullName, returnValue);
        }
Example #12
0
        public void ShouldDisposeInvokeTest()
        {
            MockDynamicInvoker mockDynamicInvoker;
            object             proxyInstance;
            Type       proxiedType;
            MethodInfo invokedMethodInfo;

            object[] invocationParameters;
            object   returnValue;

            proxyInstance     = new object();
            proxiedType       = typeof(IDisposable);
            invokedMethodInfo = (MethodInfo)MemberInfoProxy <IDisposable> .GetLastMemberInfo(exec => exec.Dispose());

            invocationParameters = new object[] { };

            mockDynamicInvoker = new MockDynamicInvoker();
            returnValue        = mockDynamicInvoker.Invoke(proxiedType, invokedMethodInfo, proxyInstance, invocationParameters);

            Assert.IsNull(returnValue);
            Assert.IsTrue(mockDynamicInvoker.Disposed);
        }
Example #13
0
        public void ShouldCloneInvokeTest()
        {
            MockDynamicInvoker mockDynamicInvoker;
            object             proxyInstance;
            Type       proxiedType;
            MethodInfo invokedMethodInfo;

            object[] invocationParameters;
            object   returnValue;

            proxyInstance     = new object();
            proxiedType       = typeof(ICloneable);
            invokedMethodInfo = (MethodInfo)MemberInfoProxy <ICloneable> .GetLastMemberInfo(exec => exec.Clone());

            invocationParameters = new object[] { };

            mockDynamicInvoker = new MockDynamicInvoker();
            returnValue        = mockDynamicInvoker.Invoke(proxiedType, invokedMethodInfo, proxyInstance, invocationParameters);

            Assert.IsNull(returnValue);
            Assert.AreEqual("ICloneable::Clone", mockDynamicInvoker.lastOperationName);
        }
        public void ShouldInvokeAsIMockObjectTest()
        {
            Mockery mockery;
            MockDynamicInvokerRealProxy mockDynamicInvokerRealProxy;
            IDynamicInvocation          mockDynamicInvocation;
            IMethodCallMessage          mockMessage;
            MethodInfo invokedMethodInfo;

            mockery = new Mockery();
            mockDynamicInvocation = mockery.NewMock <IDynamicInvocation>();
            mockMessage           = mockery.NewMock <IMethodCallMessage>();

            invokedMethodInfo = (MethodInfo)MemberInfoProxy <IMockObject> .GetLastMemberInfo(exec =>
            {
                byte bdummy   = default(byte);
                int idummy    = default(int);
                string sdummy = default(string);
                object odummy = default(object);
                bdummy        = exec.SomeMethodWithVarietyOfParameters(idummy, out sdummy, ref odummy);
            });

            Expect.Once.On(mockMessage).GetProperty("Args").Will(Return.Value(new object[] { 10, "100", (object)"1000" }));
            Expect.Exactly(2).On(mockMessage).GetProperty("MethodBase").Will(Return.Value(invokedMethodInfo));
            Expect.Once.On(mockMessage).GetProperty("LogicalCallContext").Will(Return.Value(null));
            Expect.Once.On(mockMessage).GetProperty("Uri").Will(Return.Value(null));
            Expect.Once.On(mockMessage).GetProperty("MethodName").Will(Return.Value(null));
            Expect.Once.On(mockMessage).GetProperty("TypeName").Will(Return.Value(null));
            Expect.Once.On(mockMessage).GetProperty("HasVarArgs").Will(Return.Value(false));

            mockDynamicInvokerRealProxy = new MockDynamicInvokerRealProxy(mockDynamicInvocation);
            Assert.IsNotNull(mockDynamicInvokerRealProxy);

            Expect.Once.On(mockDynamicInvocation).Method("Invoke").With(new EqualMatcher(typeof(IMockObject)), new EqualMatcher(invokedMethodInfo), new EqualMatcher(mockDynamicInvokerRealProxy.GetTransparentProxy()), new EqualMatcher(new object[] { 10, "100", (object)"1000" })).Will(Return.Value(null));

            mockDynamicInvokerRealProxy.Invoke(mockMessage);

            mockery.VerifyAllExpectationsHaveBeenMet();
        }
        public void ShouldFailOnDisposedInvokeTest()
        {
            Mockery mockery;
            MockDynamicInvokerRealProxy mockDynamicInvokerRealProxy;
            IDynamicInvocation          mockDynamicInvocation;
            IMethodCallMessage          mockMessage;
            MethodInfo invokedMethodInfo;

            mockery = new Mockery();
            mockDynamicInvocation = mockery.NewMock <IDynamicInvocation>();

            mockMessage       = mockery.NewMock <IMethodCallMessage>();
            invokedMethodInfo = (MethodInfo)MemberInfoProxy <IDisposable> .GetLastMemberInfo(exec => exec.Dispose());

            Expect.Once.On(mockDynamicInvocation).Method("Dispose").WithNoArguments();

            mockDynamicInvokerRealProxy = new MockDynamicInvokerRealProxy(mockDynamicInvocation);

            Assert.IsNotNull(mockDynamicInvokerRealProxy);

            mockDynamicInvokerRealProxy.Dispose();

            mockDynamicInvokerRealProxy.Invoke(mockMessage);
        }