private object CreateMockDelegate(Type type) { if (typeof(Delegate).Equals(type)) { throw new InvalidOperationException("The base System.Delegate type cannot be mocked."); } var types = new List <Type>(); types.Add(type); var interfaces = new List <Type>(); interfaces.Add(typeof(IMockInstance)); interfaces.Add(typeof(IMockExpectationContainer)); var instance = new MockInstance(types.ToArray()); var mockInterceptor = new MockInterceptor(instance); var proxyInterceptor = new ProxyInterceptor(instance); var dynamicType = delegateRepository.CreateTargetInterface(type); var generator = IdentifyGenerator(type); var target = generator.CreateInterfaceProxyWithoutTarget(dynamicType, interfaces.ToArray(), generatorOptions, mockInterceptor, proxyInterceptor, objectInterceptor); var proxy = Delegate.CreateDelegate(type, target, "Invoke"); instance.ProxyInstance = proxy; return(proxy); }
private object CreateMockRemoted(Type type) { var instance = new MockInstance(new Type[0]); var proxyInterceptor = new ProxyInterceptor(instance); var generator = new RepositoryForRemoting(); var proxy = generator.CreateMockRemoting(type, proxyInterceptor, instance); instance.ProxyInstance = proxy; return(proxy); }
private object CreateMockClass(Type type, Type[] extraTypes, object[] arguments, bool isPartial) { if (type.IsSealed) { throw new InvalidOperationException("Sealed classes cannot be mocked."); } var types = new List <Type>(extraTypes); types.Add(type); var interfaces = new List <Type>(extraTypes); interfaces.Add(typeof(IMockInstance)); interfaces.Add(typeof(IMockExpectationContainer)); var instance = new MockInstance(types.ToArray()); var mockInterceptor = new MockInterceptor(instance); var proxyInterceptor = new ProxyInterceptor(instance); var generator = IdentifyGenerator(type); try { var proxy = generator.CreateClassProxy(type, interfaces.ToArray(), defaultOptions, arguments, mockInterceptor, proxyInterceptor, objectInterceptor); var mocked = (IMockInstance)proxy; mocked.IsPartialInstance = isPartial; mocked.ConstructorArguments = arguments; GC.SuppressFinalize(proxy); instance.ProxyInstance = proxy; return(proxy); } catch (MissingMethodException ex) { var message = string.Format("No constructor taking {0} arguments found.", arguments.Length); throw new MissingMethodException(message, ex); } catch (TargetInvocationException ex) { var message = string.Format("Exception was thrown in constructor: {0}", ex.InnerException); throw new Exception(message, ex.InnerException); } }
private object CreateMockInterface(Type type, Type[] extraTypes) { var types = new List <Type>(extraTypes); types.Add(type); var interfaces = new List <Type>(extraTypes); interfaces.Add(typeof(IMockInstance)); interfaces.Add(typeof(IMockExpectationContainer)); var instance = new MockInstance(types.ToArray()); var mockInterceptor = new MockInterceptor(instance); var proxyInterceptor = new ProxyInterceptor(instance); var generator = IdentifyGenerator(type); var proxy = generator.CreateInterfaceProxyWithoutTarget(type, interfaces.ToArray(), generatorOptions, mockInterceptor, proxyInterceptor, objectInterceptor); instance.ProxyInstance = proxy; return(proxy); }