NUnit - ExpectedMessage 不同错误

发布于 2024-09-05 06:42:29 字数 605 浏览 2 评论 0原文

我对 TDD 很陌生,并且正在使用 NUnit 和 Moq。我有一个预计会出现异常的方法,因此我想尝试一下框架功能。

我的测试代码如下所示:

    [Test]
    [ExpectedException(ExpectedException = typeof(MockException), ExpectedMessage = "Actual differs from expected")]
    public void Write_MessageLogWithCategoryInfoFail()
    {
        string message = "Info Test Message";

        Write_MessageLogWithCategory(message, "Info");

        _LogTest.Verify(writeMessage =>
            writeMessage.Info("This should fail"),
            "Actual differs from expected"
        );
    }

但我总是收到错误消息,指出实际异常消息与预期消息不同的错误消息。我做错了什么?

I am quite new to TDD and am going with NUnit and Moq. I have got a method where I expect an exception, so I wanted to play a little with the frameworks features.

My test code looks as follows:

    [Test]
    [ExpectedException(ExpectedException = typeof(MockException), ExpectedMessage = "Actual differs from expected")]
    public void Write_MessageLogWithCategoryInfoFail()
    {
        string message = "Info Test Message";

        Write_MessageLogWithCategory(message, "Info");

        _LogTest.Verify(writeMessage =>
            writeMessage.Info("This should fail"),
            "Actual differs from expected"
        );
    }

But I always receive the errormessage that the error message that the actual exception message differs from the expected message. What am I doing wrong?

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

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

发布评论

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

评论(2

情绪失控 2024-09-12 06:42:29

不幸的是,Resharper 测试运行程序有一个错误/限制 - 它不处理 ExpectedException 属性。您有 2 个选择:

  1. 使用其他测试运行程序(例如 nunit-gui.exe,随 NUnit 一起提供) - 但这种方法使调试测试变得很痛苦

  2. 使用以下模式手动捕获并验证异常:

    [测试]
    公共无效Write_MessageLogWithCategoryInfoFail()
    {
    尝试
    {
    string message = "信息测试消息";

     Write_MessageLogWithCategory(message, "Info");
    
      _LogTest.Verify(writeMessage =>;
          writeMessage.Info("这应该失败"),
          “实际情况与预期不同”
      );
      Assert.Fail("预期异常");
    }
    捕获(MockException e)
    {
      Assert.AreEqual("实际与预期不同", e.Message);
    }
    

    }

真是太遗憾了,因为用描述性的方式来表达你期待一个异常要好得多!

附带说明一下,我希望上面的代码仅用于使用框架 - 通常你永远不会捕获 MockExceptions :)

Unfortunately Resharper test runner has a bug/limitation - it doesn't handle the ExpectedException attributes. You have 2 options:

  1. Use some other test runner (e.g. nunit-gui.exe, shipped with NUnit) - but this approach makes it a pain to debug your tests

  2. Catch and validate the exception manually, using the following pattern:

    [Test]
    public void Write_MessageLogWithCategoryInfoFail()
    {
    try
    {
    string message = "Info Test Message";

      Write_MessageLogWithCategory(message, "Info");
    
      _LogTest.Verify(writeMessage =>
          writeMessage.Info("This should fail"),
          "Actual differs from expected"
      );
      Assert.Fail("Expected exception");
    }
    catch(MockException e)
    {
      Assert.AreEqual("Actual differs from expected", e.Message);
    }
    

    }

Its a real shame, because the descriptive way of saying that you expect an exception is much nicer!

On a side note I hope that the code above is only for playing with the framework - usually you would never catch MockExceptions :)

身边 2024-09-12 06:42:29

您可以使用额外的参数MatchType = MessageMatch.Regex

You may use the extra parameter MatchType = MessageMatch.Regex.

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