public void BuildRuntimeType_MethodInterceptors_InterceptInterfaceMethod()
        {
            // Arrange
            Mock <IMockableRuntimeTypePropertyManager> managerMock = new Mock <IMockableRuntimeTypePropertyManager>(MockBehavior.Strict);
            var        manager   = managerMock.Object;
            MethodInfo getMethod = manager.GetType().GetMethod("GetValue");
            MethodInfo setMethod = manager.GetType().GetMethod("SetValue");

            bool   interceptorCalled = false;
            Action interceptor       = () => interceptorCalled = true;

            MethodInterceptions interceptions = new MethodInterceptions
            {
                Interceptions = new MethodInterception[]
                {
                    new DelegateMethodInterception
                    {
                        InterceptedMethod = typeof(IInterfaceWithMethod).GetMethod(nameof(IInterfaceWithMethod.MethodToIntercept)),
                        Delegate          = interceptor
                    }
                }
            };

            // Act
            Stopwatch            sw          = Stopwatch.StartNew();
            Type                 runtimeType = RuntimeProxyBuilder.BuildRuntimeType(typeof(IInterfaceWithMethod), getMethod, setMethod, interceptions);
            IInterfaceWithMethod impl        = (IInterfaceWithMethod)Activator.CreateInstance(runtimeType, new object[] { manager, interceptions });

            impl.MethodToIntercept();
            sw.Stop();
            Trace.WriteLine(sw.ElapsedMilliseconds);

            // Assert
            Assert.IsTrue(interceptorCalled);
        }
Example #2
0
        public void WithConfigureMembers_IsNotExpectedToReturnValueInReceivedInOrderBlock()
        {
            // Arrange
            var fixture = new Fixture().Customize(new AutoNSubstituteCustomization {
                ConfigureMembers = true
            });
            var substitute = fixture.Create <IInterfaceWithMethodReturningOtherInterface>();

            var actualResult = substitute.Method();

            // Act
            IInterfaceWithMethod capturedResult = null;

            Received.InOrder(() => { capturedResult = substitute.Method(); });

            // Assert
            Assert.NotEqual(actualResult, capturedResult);
        }
Example #3
0
        public void IsNotExpectedToReturnValueInReceivedInOrderBlock()
        {
            // Fixture setup
            var fixture    = new Fixture().Customize(new AutoConfiguredNSubstituteCustomization());
            var substitute = fixture.Create <IInterfaceWithMethodReturningOtherInterface>();

            var actualResult = substitute.Method();

            // Exercise system
            IInterfaceWithMethod capturedResult = null;

            Received.InOrder(() =>
            {
                capturedResult = substitute.Method();
            });

            // Verify outcome
            Assert.NotEqual(actualResult, capturedResult);

            // Teardown
        }