You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Matt Young <ma...@gmail.com> on 2006/12/03 09:02:48 UTC

Fwd: Problem mocking log4net appender

---------- Forwarded message ----------
From: Matt Young <ma...@gmail.com>
Date: Nov 14, 2006 8:01 PM
Subject: Problem mocking log4net appender
To: nmock-general@lists.sourceforge.net


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


-- 
Matt