public void CanUseBackToRecordOnMethodsThatCallToCallOriginalMethod()
        {
            MockRepository repository = new MockRepository();
            TestClass      mock       = repository.StrictMock <TestClass>();

            mock.Method();
            LastCall.CallOriginalMethod
                (OriginalCallOptions.NoExpectation);

            repository.ReplayAll();
            mock.Method();
            repository.VerifyAll();

            repository.BackToRecordAll();

            mock.Method();
            LastCall.Throw(new ApplicationException());

            repository.ReplayAll();

            try
            {
                mock.Method();
                Assert.False(true);
            }
            catch
            {
            }
            repository.VerifyAll();
        }
        /// <summary>
        /// This test ensures, that for a given loglevel
        /// a) <c>AbstractLogger.Write</c> is not called if that loglevel is disabled
        /// b) No argument is evaluated (e.g. calling ToString()) if that loglevel is disabled
        /// </summary>
        private static void WriteAndEvaluateOnlyWhenLevelEnabled(string levelName)
        {
            MockRepository mocks = new MockRepository();

            AbstractLogger        log           = (AbstractLogger)mocks.StrictMock(typeof(AbstractLogger));
            Exception             ex            = (Exception)mocks.StrictMock(typeof(Exception));
            object                messageObject = mocks.StrictMock(typeof(object));
            object                formatArg     = mocks.StrictMock(typeof(object));
            FormatMessageCallback failCallback  = TestFormatMessageCallback.FailCallback();

            MethodInfo[] logMethods = GetLogMethodSignatures(levelName);

            using (mocks.Ordered())
            {
                Invoke(log, logMethods[0], messageObject);
                LastCall.CallOriginalMethod(OriginalCallOptions.CreateExpectation);
                Expect.Call(IsLevelEnabled(log, levelName)).Return(false);
                Invoke(log, logMethods[1], messageObject, ex);
                LastCall.CallOriginalMethod(OriginalCallOptions.CreateExpectation);
                Expect.Call(IsLevelEnabled(log, levelName)).Return(false);
                Invoke(log, logMethods[2], "format", new object[] { formatArg });
                LastCall.CallOriginalMethod(OriginalCallOptions.CreateExpectation);
                Expect.Call(IsLevelEnabled(log, levelName)).Return(false);
                Invoke(log, logMethods[3], "format", ex, new object[] { formatArg });
                LastCall.CallOriginalMethod(OriginalCallOptions.CreateExpectation);
                Expect.Call(IsLevelEnabled(log, levelName)).Return(false);
                Invoke(log, logMethods[4], CultureInfo.InvariantCulture, "format", new object[] { formatArg });
                LastCall.CallOriginalMethod(OriginalCallOptions.CreateExpectation);
                Expect.Call(IsLevelEnabled(log, levelName)).Return(false);
                Invoke(log, logMethods[5], CultureInfo.InvariantCulture, "format", ex, new object[] { formatArg });
                LastCall.CallOriginalMethod(OriginalCallOptions.CreateExpectation);
                Expect.Call(IsLevelEnabled(log, levelName)).Return(false);
                Invoke(log, logMethods[6], failCallback);
                LastCall.CallOriginalMethod(OriginalCallOptions.CreateExpectation);
                Expect.Call(IsLevelEnabled(log, levelName)).Return(false);
                Invoke(log, logMethods[7], failCallback, ex);
                LastCall.CallOriginalMethod(OriginalCallOptions.CreateExpectation);
                Expect.Call(IsLevelEnabled(log, levelName)).Return(false);
                Invoke(log, logMethods[8], CultureInfo.InvariantCulture, failCallback);
                LastCall.CallOriginalMethod(OriginalCallOptions.CreateExpectation);
                Expect.Call(IsLevelEnabled(log, levelName)).Return(false);
                Invoke(log, logMethods[9], CultureInfo.InvariantCulture, failCallback, ex);
                LastCall.CallOriginalMethod(OriginalCallOptions.CreateExpectation);
                Expect.Call(IsLevelEnabled(log, levelName)).Return(false);
            }
            mocks.ReplayAll();

            Invoke(log, logMethods[0], messageObject);
            Invoke(log, logMethods[1], messageObject, ex);
            Invoke(log, logMethods[2], "format", new object[] { formatArg });
            Invoke(log, logMethods[3], "format", ex, new object[] { formatArg });
            Invoke(log, logMethods[4], CultureInfo.InvariantCulture, "format", new object[] { formatArg });
            Invoke(log, logMethods[5], CultureInfo.InvariantCulture, "format", ex, new object[] { formatArg });
            Invoke(log, logMethods[6], failCallback);
            Invoke(log, logMethods[7], failCallback, ex);
            Invoke(log, logMethods[8], CultureInfo.InvariantCulture, failCallback);
            Invoke(log, logMethods[9], CultureInfo.InvariantCulture, failCallback, ex);

            mocks.VerifyAll();
        }
 [Test] public void WhenConnectionClose()
 {
     _listener.AfterStateChange(_connection, ConnectionState.Closed, ConnectionState.Open);
     LastCall.CallOriginalMethod(OriginalCallOptions.NoExpectation);
     _listener.AfterConnectionClose(_connection);
     LastCall.CallOriginalMethod(OriginalCallOptions.CreateExpectation);
     _mockery.ReplayAll();
     _listener.AfterStateChange(_connection, ConnectionState.Open, ConnectionState.Closed);
     _mockery.VerifyAll();
 }
 [Test] public void WhenConnectionNeitherOpenNorClose()
 {
     _listener.AfterStateChange(_connection, ConnectionState.Open, ConnectionState.Open);
     LastCall.CallOriginalMethod(OriginalCallOptions.NoExpectation).Repeat.Any();
     _mockery.ReplayAll();
     _listener.AfterStateChange(_connection, ConnectionState.Open, ConnectionState.Fetching);
     _listener.AfterStateChange(_connection, ConnectionState.Fetching, ConnectionState.Open);
     _listener.AfterStateChange(_connection, ConnectionState.Open, ConnectionState.Broken);
     _listener.AfterStateChange(_connection, ConnectionState.Fetching, ConnectionState.Broken);
     _mockery.VerifyAll();
 }
Beispiel #5
0
        public void LastCallCallOriginalMethod()
        {
            CallOriginalMethodFodder comf1 = (CallOriginalMethodFodder)mocks.DynamicMock(typeof(CallOriginalMethodFodder));
            CallOriginalMethodFodder comf2 = (CallOriginalMethodFodder)mocks.DynamicMock(typeof(CallOriginalMethodFodder));

            comf2.TheMethod();
            LastCall.CallOriginalMethod(OriginalCallOptions.CreateExpectation);

            mocks.ReplayAll();

            comf1.TheMethod();
            Assert.Equal(false, comf1.OriginalMethodCalled);

            comf2.TheMethod();
            Assert.Equal(true, comf2.OriginalMethodCalled);
        }
        public void InvokeAttributedMethod_UsesCache()
        {
            ClassWithDeserializationEvents instance = new ClassWithDeserializationEvents();
            StreamingContext context = new StreamingContext();

            MockRepository           repository      = new MockRepository();
            SerializationEventRaiser eventRaiserMock = repository.StrictMock <SerializationEventRaiser>();

            eventRaiserMock.InvokeAttributedMethod(instance, typeof(OnDeserializedAttribute), context);
            LastCall.CallOriginalMethod(OriginalCallOptions.CreateExpectation);

            Expect.Call(PrivateInvoke.InvokeNonPublicMethod(eventRaiserMock, "FindDeserializationMethodsWithCache", typeof(ClassWithDeserializationEvents), typeof(OnDeserializedAttribute)))
            .Return(new List <MethodInfo>());

            repository.ReplayAll();

            eventRaiserMock.InvokeAttributedMethod(instance, typeof(OnDeserializedAttribute), context);

            repository.VerifyAll();
        }
Beispiel #7
0
        public void LastCallOriginalMethod_WithExpectation()
        {
            MockRepository           mockRepository = new MockRepository();
            CallOriginalMethodFodder comf1          = (CallOriginalMethodFodder)mockRepository.DynamicMock(typeof(CallOriginalMethodFodder));
            CallOriginalMethodFodder comf2          = (CallOriginalMethodFodder)mockRepository.DynamicMock(typeof(CallOriginalMethodFodder));

            comf2.TheMethod();
            LastCall.CallOriginalMethod(OriginalCallOptions.CreateExpectation)
            .Repeat.Twice();

            mockRepository.ReplayAll();

            comf1.TheMethod();
            Assert.Equal(false, comf1.OriginalMethodCalled);

            comf2.TheMethod();
            Assert.Equal(true, comf2.OriginalMethodCalled);

            Assert.Throws <ExpectationViolationException>("CallOriginalMethodFodder.TheMethod(); Expected #2, Actual #1.",
                                                          () => mockRepository.VerifyAll());
        }