Esempio n. 1
0
        /// <summary>
        /// Creates a mocked instance for the given serverType.
        /// </summary>
        /// <param name="serverType">The type for which to create an instance.
        /// Should be the type decorated by this attribute.</param>
        protected virtual MarshalByRefObject CreateMockedInstance(Type serverType)
        {
            IMocker customMocker;

            // Retrieve customMockerType from name:
            if (this.customMockerType == null)
            {
                this.customMockerType = Type.GetType(customMockerTypeName);
            }
            // Check customMockerType exists:
            if (this.customMockerType == null)
            {
                throw new TypeLoadException(String.Format("The typename \"{0}\" configured as mocker on a CustomMockAttribute could not be resolved.", this.customMockerTypeName));
            }
            // Create custom mocker:
            customMocker = (IMocker)customMockerType.GetConstructor(new Type[] {}).Invoke(new object[] {});
            // Create proxy, return transparent proxy:
            RealProxy rp;

            if (RecorderManager.IsRecording)
            {
                rp = new MockingProxy(serverType, customMocker, RecorderManager.GetNextInstanceName(serverType));
            }
            else
            {
                rp = new MockingProxy(serverType, customMocker, null);
            }
            return((MarshalByRefObject)rp.GetTransparentProxy());
        }
Esempio n. 2
0
        /// <summary>
        /// Creates an eventually mocked instance for the given serverType.
        /// </summary>
        /// <param name="serverType">The type for which to create an instance.
        /// Should be the type decorated by this attribute.</param>
        protected override MarshalByRefObject CreateMockedInstance(Type serverType)
        {
            RealProxy rp;

            switch (RecorderManager.Action)
            {
            case RecorderState.Recording:
                // Create a recording proxy for the object:
                MarshalByRefObject target = CreateUnmockedInstance(serverType);
                rp = new MockingProxy(serverType, new RecordingMocker(target), RecorderManager.GetNextInstanceName(serverType));
                return((MarshalByRefObject)rp.GetTransparentProxy());

            case RecorderState.PlayBack:
                // Create a playback proxy:
                rp = new MockingProxy(serverType, new PlayBackMocker(), null);
                return((MarshalByRefObject)rp.GetTransparentProxy());

            default:
                // Create an instance without proxy:
                return(CreateUnmockedInstance(serverType));
            }
        }
Esempio n. 3
0
 /// <summary>
 /// If the passed value is serializable, returns it as is, otherwise wraps it in a recording proxy.
 /// </summary>
 private static object WrapObject(object value, Type expectedType)
 {
     if (value == null)
     {
         // Leave result as is
         return(null);
     }
     else if (RemotingServices.IsObjectOutOfAppDomain(value))
     {
         // Wrap the result in a RecordingProxy on the method's returntype:
         return(new MockingProxy(expectedType, new RecordingMocker((MarshalByRefObject)value), RecorderManager.GetNextInstanceName(expectedType)).GetTransparentProxy());
     }
     else if (value.GetType().IsMarshalByRef)
     {
         // Wrap the result in a RecordingProxy on the objects real type:
         return(new MockingProxy(value.GetType(), new RecordingMocker((MarshalByRefObject)value), RecorderManager.GetNextInstanceName(value.GetType())).GetTransparentProxy());
     }
     else if (value.GetType().IsSerializable)
     {
         // Leave result as is
         return(value);
     }
     else
     {
         // When result is neither serializable, nor MarchalByRef, throw:
         throw new TypeMockException(value.GetType());
     }
 }