logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Matt Young" <matt.young...@gmail.com>
Subject Problem mocking log4net appender
Date Tue, 05 Dec 2006 00:21:07 GMT
Greetings :)

I encountered a problem with NMock2 (1.0.2313.18049) while attempting
to mock a log4net (1.2.10.0) appender. The mock appender throws an
ExpectionException when log4net calls DoAppend and then log4net eats
the exception the test finishes.  The problem is
VerifyAllExpectationsHaveBeenMet() does not cause the test to fail.
Apparently the mock appender does not remember that the Expect.Never
expectation was broken.

Can anyone suggest a workaround to this problem? Thanks :)

Here's the code,
--------------------------------------------------------------
using System;
using NUnit.Framework;
using NMock2;

namespace LoggerTest
{
    public class Worker
    {
        private static readonly log4net.ILog logger =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public void DoWork()
        {
            logger.Error("oops.");
        }
    }

    [TestFixture]
    public class Tester
    {
        private Mockery mockery;
        private log4net.Appender.IAppender appender;

        [SetUp] public void SetUp()
        {
            mockery = new Mockery();
            appender = mockery.NewMock<log4net.Appender.IAppender>();

            log4net.Config.BasicConfigurator.Configure(appender);
            log4net.Repository.Hierarchy.Hierarchy rep =
(log4net.Repository.Hierarchy.Hierarchy)
log4net.LogManager.GetRepository();
            rep.Threshold = log4net.Core.Level.Info;

            Expect.Never.On(appender).Method("DoAppend").WithAnyArguments();
        }

        [TearDown] public void TearDown()
        {
            log4net.Repository.Hierarchy.Hierarchy rep =
(log4net.Repository.Hierarchy.Hierarchy)
log4net.LogManager.GetRepository();
            rep.Root.RemoveAppender(appender);
            rep.Configured = false;
        }

        [Test] public void Test1()
        {
            Worker worker = new Worker();
            worker.DoWork();
            mockery.VerifyAllExpectationsHaveBeenMet();
        }
    }
}

---------------------------------------------------
------ Test started: Assembly: LoggerTest.dll ------

Error: log4net:ERROR Log: Exception while logging
log4net:ERROR Log: Exception while logging
Error: NMock2.Internal.ExpectationException: unexpected invocation of
appender.Name
Expected:

   at NMock2.Mockery.FailUnexpectedInvocation(Invocation invocation)
   at NMock2.Mockery.Dispatch(Invocation invocation)
   at NMock2.Mockery.MockObject.Invoke(Invocation invocation)
   at NMock2.Monitoring.Invoker.Invoke(Invocation invocation)
   at NMock2.Monitoring.ProxiedObjectIdentity.Invoke(Invocation invocation)
   at NMock2.Monitoring.ProxyInvokableAdapter.Invoke(IMessage msg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&
msgData, Int32 type)
   at log4net.Appender.IAppender.get_Name()
   at log4net.Util.AppenderAttachedImpl.AppendLoopOnAppenders(LoggingEvent
loggingEvent)
   at log4net.Repository.Hierarchy.Logger.CallAppenders(LoggingEvent
loggingEvent)
   at log4net.Repository.Hierarchy.Logger.ForcedLog(Type
callerStackBoundaryDeclaringType, Level level, Object message,
Exception exception)
   at log4net.Repository.Hierarchy.Logger.Log(Type
callerStackBoundaryDeclaringType, Level level, Object message,
Exception exception)
NMock2.Internal.ExpectationException: unexpected invocation of appender.Name
Expected:

   at NMock2.Mockery.FailUnexpectedInvocation(Invocation invocation)
   at NMock2.Mockery.Dispatch(Invocation invocation)
   at NMock2.Mockery.MockObject.Invoke(Invocation invocation)
   at NMock2.Monitoring.Invoker.Invoke(Invocation invocation)
   at NMock2.Monitoring.ProxiedObjectIdentity.Invoke(Invocation invocation)
   at NMock2.Monitoring.ProxyInvokableAdapter.Invoke(IMessage msg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&
msgData, Int32 type)
   at log4net.Appender.IAppender.get_Name()
   at log4net.Util.AppenderAttachedImpl.AppendLoopOnAppenders(LoggingEvent
loggingEvent)
   at log4net.Repository.Hierarchy.Logger.CallAppenders(LoggingEvent
loggingEvent)
   at log4net.Repository.Hierarchy.Logger.ForcedLog(Type
callerStackBoundaryDeclaringType, Level level, Object message,
Exception exception)
   at log4net.Repository.Hierarchy.Logger.Log(Type
callerStackBoundaryDeclaringType, Level level, Object message,
Exception exception)

1 passed, 0 failed, 0 skipped, took 0.98 seconds.

-- 
Matt

Mime
View raw message