public override void TestSetUp() { MockPart.Setup(p => p.GetExportedValue(OrderProcessorExportDefinition)).Returns(interceptedOrderProcessor); InterceptedPart = MockPart.Object; MockInterceptor.Setup(p => p.Intercept(interceptedOrderProcessor)).Returns(interceptingOrderProcessor); InterceptingPart = new InterceptingComposablePart(InterceptedPart, MockInterceptor.Object); }
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); }
public void custom_interceptor_for_all() { var interceptor = new MockInterceptor(); IService service = getService("Green", r => { r.ForRequestedType <IService>().InterceptWith(interceptor) .AddInstances(x => { x.ConstructedBy(() => new ColorService("Green")).WithName("Green"); }); }); interceptor.Target.ShouldBeTheSameAs(service); }
public void custom_interceptor_for_all() { var interceptor = new MockInterceptor(); IService service = getService("Green", r => { r.ForRequestedType<IService>().InterceptWith(interceptor) .AddInstances(x => { x.ConstructedBy(() => new ColorService("Green")).WithName("Green"); }); }); interceptor.Target.ShouldBeTheSameAs(service); }
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); }
public void It_should_pass_the_value_to_the_value_interceptor() { InterceptingPart.GetExportedValue(OrderProcessorExportDefinition); MockInterceptor.Verify(p => p.Intercept(interceptedOrderProcessor)); }
public void It_should_only_invoke_the_interceptor_once() { MockInterceptor.Verify(p => p.Intercept(interceptedOrderProcessor), Times.Once()); }