【问题标题】:How to construct unit test如何构建单元测试
【发布时间】:2010-10-04 14:06:01
【问题描述】:

我有自定义事件记录器,我想对其进行测试。

[Test]
        public void AddLogWithExceptionAndEventLogEntryTypeTest()
        {

        const string loggerName = "mockLoggerName";
        var logger = new MyLogger(loggerName);

        System.Diagnostics.EventLog log = new System.Diagnostics.EventLog("System");
        string logValue = Guid.NewGuid().ToString();
        logger.AddEntry(new Exception(logValue),EventLogEntryType.Error );


        foreach (
            EventLogEntry entry in log.Entries)
        {
            if (entry.Message.ToUpper().Contains(logValue))
            {
              //what can is do ?
            }
        }
    }

使用什么断言来提供信息,添加了那个条目?

【问题讨论】:

标签: c# unit-testing nunit rhino-mocks event-log


【解决方案1】:

您是否打算在日志中查看您刚刚添加的文本?那么怎么样:

bool foundOne = false;
foreach (EventLogEntry entry in log.Entries)
    {
        if (entry.Message.ToUpper().Contains(logValue))
        {
          foundOne = true;
        }
    }

Assert(foundOne);

就个人而言,我可能会模拟记录器,而是断言模拟记录器的方法按预期调用。

【讨论】:

  • +1:您无需测试写入日志消息的 .NET 框架代码,只需您的类使用预期参数调用这些方法即可。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-17
  • 2010-12-03
  • 1970-01-01
  • 2012-11-15
  • 2010-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多