简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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