如何构建单元测试

时间:2010-10-04 14:06:01

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

我有自定义事件记录器,我想测试它。

[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 ?
            }
        }
    }

用什么断言来提供信息,该条目已被添加?

1 个答案:

答案 0 :(得分:3)

您是否打算通过日志查看刚刚添加的文字?那怎么样:

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

Assert(foundOne);

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