简体   繁体   English

如何构建单元测试

[英]How to construct unit test

I have custom event logger, and I want to test it. 我有自定义事件记录器,我想对其进行测试。

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

What assert to use to give information, that entry was added ? 用来添加信息的断言是什么?

Is your intent to look through the log for text you just added? 您是否打算在日志中查找刚添加的文本? Then how about: 然后如何:

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

Assert(foundOne);

Personally, I would instead probably mock the logger, and instead assert that the methods of the mocked logger were called as expected. 就个人而言,我可能会模拟记录器,而是断言模拟记录器的方法已按预期方式调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM