示例#1
0
        /// <summary>
        /// Constructs an object or a mocker (depending on the mocking framework state).
        /// </summary>
        /// <param name="instanceName">The instancename of the mock.</param>
        /// <param name="typeToConstruct">The type of object to construct.</param>
        /// <param name="typeOfMock">The type of mock to create (should be either the typeToConstruct if it is MarshalByRef, or an interface it supports).</param>
        /// <param name="constructorArguments">Arguments of the constructor.</param>
        /// <returns>Either an instance of the typeToConstruct, or a mock for it.</returns>
        /// <remarks>
        /// This method will create either a plain instance of typeToConstruct, a recording
        /// mock wrapping a plain instance of typeToConstruct, or a playback mock with no plain instance.
        /// Use this method as an alternative to factory implementations, where you need to be
        /// able to mock an instance created, but can't use the AutoMock or CustomMock attributes.
        /// Note that the typeToConstruct type must be registered to the MockService as a type to mock !
        /// </remarks>
        public static object Construct(string instanceName, Type typeToConstruct, Type typeOfMock, params object[] constructorArguments)
        {
            object result = null;

            if ((RecorderManager.IsPlaying) && MockService.IsTypeToMock(typeToConstruct))
            {
                // If playback, no instance to create, just a playback mock:
                result = (new MockingProxy(typeOfMock, new PlayBackMocker(), instanceName)).GetTransparentProxy();
            }
            else
            {
                // Otherwise, a real instance should be created:
                result = typeToConstruct.InvokeMember(null, BindingFlags.CreateInstance, null, null, constructorArguments);
                if ((RecorderManager.IsRecording) && MockService.IsTypeToMock(typeToConstruct))
                {
                    // If recording, wrap the instance in a recording mock:
                    result = (new MockingProxy(typeOfMock, new RecordingMocker(result), instanceName)).GetTransparentProxy();
                }
            }
            return(result);
        }
示例#2
0
 /// <summary>
 /// Checks whether or not instances of the given serverType should be mocked.
 /// </summary>
 /// <param name="serverType">The type for which to create an instance.
 /// Should be the type decorated by this attribute.</param>
 protected virtual bool IsTypeToMock(Type serverType)
 {
     return(MockService.IsTypeToMock(serverType));
 }