模拟对象,nUnit,调用日志和log4net

问题描述:

单元测试的一种非常常见的情况如下:

A very often scenario of unit test is as follows:

public void SetUp()
{
  this.callLog = new StringBuilder();
}

public void TestBuzzBar()
{
  var bar = new Bar(new MockFoo(callLog));
  bar.Buzz(17);
  Assert.AreEqual("MockFoo.Init(17) MockFoo.PrepareStuff MockFoo.DoTheJob ", callLog.ToString());
}

... MockFoo通过仅将字符串附加到呼叫日志来实现IFoo接口.它需要使用模拟中的callLog进行大量代码处理.

... with MockFoo implementing an IFoo interface by just appending strings a call log. It requires a lot of code handling with callLog in mocks.

使用log4net收集呼叫日志是个好主意吗?

Is it a good idea to use log4net to collect call log?

要回答您的问题:log4net是一个很棒的日志记录框架.易于设置和使用.我已经在带有MemoryAppender的单元测试中使用了它,从本质上来说,它使您可以回头查看测试过程中记录的内容.该技术适用于模拟和SUT.

To answer your question: log4net is a great logging framework. It is easy to set up and use. I have used it in unit tests with a MemoryAppender which essentially enables you to go back and peek at what have been logged during the test. This technique works for both mocks and SUT.