如何构建单元测试

发布于 2024-09-26 04:06:41 字数 690 浏览 6 评论 0原文

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

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

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

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

<逆流佳人身旁 2024-10-03 04:06:41

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

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

Assert(foundOne);

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文