mock ReadOnly property ?

57 views
Skip to first unread message

tom308

unread,
Nov 23, 2009, 8:46:16 AM11/23/09
to Rhino.Mocks
Hi all!
i found few solution to this issue that didn't solve my problem.
i am building a log4net wrapper, and i am testing it with MSTest &
Rhino mock 3.5.

my problem is that i am trying to mock the LogManager.GetLogger
().Logger.IsEnabledFor().
i get object reference exception when trying to do:

rootLogger.Stub(l => l.Logger.IsEnabledFor(Level.Fatal)).Return(true);

and i can't do something like this:

rootLogger.Logger = new RootLogger();

because Logger is readOnly.

can anyone help?

Patrick Steele

unread,
Nov 23, 2009, 10:48:32 AM11/23/09
to rhino...@googlegroups.com
LogManager.GetLogger() is static and therefore can't be intercepted by
Rhino Mocks. You may want to consider wrapping the functionality of
your logging in your own interfaces. You can mock out those
interfaces during testing, and implement a log4net implementation of
those interfaces for runtime.

---
Patrick Steele
http://weblogs.asp.net/psteele

tom308

unread,
Nov 23, 2009, 10:58:08 AM11/23/09
to Rhino.Mocks
sorry if i was misunderstood.
i don't need the static call - i have a ILog instance and i want to
mock the <ILog instance>.Logger.IsEnabledFor().

Thanks
Tom

On Nov 23, 5:48 pm, Patrick Steele <patrick.ste...@gmail.com> wrote:
> LogManager.GetLogger() is static and therefore can't be intercepted by
> Rhino Mocks.  You may want to consider wrapping the functionality of
> your logging in your own interfaces.  You can mock out those
> interfaces during testing, and implement a log4net implementation of
> those interfaces for runtime.
>
> ---
> Patrick Steelehttp://weblogs.asp.net/psteele

Patrick Steele

unread,
Nov 23, 2009, 11:12:03 AM11/23/09
to rhino...@googlegroups.com
Oops. Okay. I see. Something like this should work:

ILog log = MockRepository.GenerateStub<ILog>();
ILogger logger = MockRepository.GenerateStub<ILogger>();
logger.Stub(l => l.IsEnabledFor(Level.FATAL)).Return(true);
log.Stub(l => l.Logger).Return(logger);

---
Patrick Steele
http://weblogs.asp.net/psteele

tom308

unread,
Nov 24, 2009, 10:18:24 AM11/24/09
to Rhino.Mocks
thanks a lot Patrick - that's exactly what i was looking for (for too
much time) !!!

Tom

On Nov 23, 6:12 pm, Patrick Steele <patrick.ste...@gmail.com> wrote:
> Oops.  Okay.  I see.  Something like this should work:
>
>                         ILog log = MockRepository.GenerateStub<ILog>();
>                         ILogger logger = MockRepository.GenerateStub<ILogger>();
>                         logger.Stub(l => l.IsEnabledFor(Level.FATAL)).Return(true);
>                         log.Stub(l => l.Logger).Return(logger);
>
> ---
> Patrick Steelehttp://weblogs.asp.net/psteele
Reply all
Reply to author
Forward
0 new messages