<bdo id='WITy0'></bdo><ul id='WITy0'></ul>
    <tfoot id='WITy0'></tfoot>

    1. <legend id='WITy0'><style id='WITy0'><dir id='WITy0'><q id='WITy0'></q></dir></style></legend>
    2. <small id='WITy0'></small><noframes id='WITy0'>

    3. <i id='WITy0'><tr id='WITy0'><dt id='WITy0'><q id='WITy0'><span id='WITy0'><b id='WITy0'><form id='WITy0'><ins id='WITy0'></ins><ul id='WITy0'></ul><sub id='WITy0'></sub></form><legend id='WITy0'></legend><bdo id='WITy0'><pre id='WITy0'><center id='WITy0'></center></pre></bdo></b><th id='WITy0'></th></span></q></dt></tr></i><div id='WITy0'><tfoot id='WITy0'></tfoot><dl id='WITy0'><fieldset id='WITy0'></fieldset></dl></div>
    4. NUnit 预期异常

      时间:2023-05-20

      <small id='GFYwm'></small><noframes id='GFYwm'>

          <i id='GFYwm'><tr id='GFYwm'><dt id='GFYwm'><q id='GFYwm'><span id='GFYwm'><b id='GFYwm'><form id='GFYwm'><ins id='GFYwm'></ins><ul id='GFYwm'></ul><sub id='GFYwm'></sub></form><legend id='GFYwm'></legend><bdo id='GFYwm'><pre id='GFYwm'><center id='GFYwm'></center></pre></bdo></b><th id='GFYwm'></th></span></q></dt></tr></i><div id='GFYwm'><tfoot id='GFYwm'></tfoot><dl id='GFYwm'><fieldset id='GFYwm'></fieldset></dl></div>
            <tbody id='GFYwm'></tbody>

          <tfoot id='GFYwm'></tfoot>
            • <bdo id='GFYwm'></bdo><ul id='GFYwm'></ul>

              <legend id='GFYwm'><style id='GFYwm'><dir id='GFYwm'><q id='GFYwm'></q></dir></style></legend>

                本文介绍了NUnit 预期异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我有一组测试用例,其中一些预计会引发异常.因此,我已将这些测试的属性设置为预期会出现如下异常:

                I've got a set of test cases, some of which are expected to throw exceptions. Because of this, I have have set the attributes for these tests to expect exceptions like so:

                [ExpectedException("System.NullReferenceException")]
                

                当我在本地运行测试时,一切都很好.但是,当我将测试转移到运行 TeamCity 的 CI 服务器时,所有预期异常的测试都会失败.这是一个已知的错误.

                When I run my tests locally all is good. However when I move my tests over to the CI server running TeamCity, all my tests that have expected exceptions fail. This is a known bug.

                我知道 NUnit 还提供了 Assert.Throws<>Assert.Throws 方法.

                I am aware that there is also the Assert.Throws<> and Assert.Throws methods that NUnit offers.

                我的问题是如何使用这些而不是我当前使用的属性?

                My question is how can I make use of these instead of the attribute I'm currently using?

                我查看了 StackOverflow 并尝试了一些似乎对我不起作用的方法.

                I've had a look around StackOverflow and tried a few things none of which seem to work for me.

                有没有简单的 1 行解决方案来使用它?

                Is there a simple 1 line solution to using this?

                推荐答案

                我不确定你的尝试是否会给你带来麻烦,但你可以简单地将 lambda 作为第一个参数传递给 Assert.Throws.这是我通过的一项测试中的一项:

                I'm not sure what you've tried that is giving you trouble, but you can simply pass in a lambda as the first argument to Assert.Throws. Here's one from one of my tests that passes:

                Assert.Throws<ArgumentException>(() => pointStore.Store(new[] { firstPoint }));
                

                <小时>

                好的,这个例子可能有点冗长.假设我有一个测试


                Okay, that example may have been a little verbose. Suppose I had a test

                [Test]
                [ExpectedException("System.NullReferenceException")]
                public void TestFoo()
                {
                    MyObject o = null;
                    o.Foo();
                }
                

                这会正常通过,因为 o.Foo() 会引发空引用异常.

                which would pass normally because o.Foo() would raise a null reference exception.

                然后您将删除 ExpectedException 属性并将您对 o.Foo() 的调用包装在 Assert.Throws 中.

                You then would drop the ExpectedException attribute and wrap your call to o.Foo() in an Assert.Throws.

                [Test]
                public void TestFoo()
                {
                    MyObject o = null;
                    Assert.Throws<NullReferenceException>(() => o.Foo());
                }
                

                Assert.Throws尝试调用以委托表示的代码片段,以验证它是否引发了特定异常."() =>DoSomething() 语法表示一个 lambda,本质上是一个匿名方法.所以在这种情况下,我们告诉 Assert.Throws 执行代码片段 o.Foo().

                Assert.Throws "attempts to invoke a code snippet, represented as a delegate, in order to verify that it throws a particular exception." The () => DoSomething() syntax represents a lambda, essentially an anonymous method. So in this case, we are telling Assert.Throws to execute the snippet o.Foo().

                所以不,你不能像添加属性那样只添加一行;您需要在对 Assert.Throws 的调用中显式包装将引发异常的测试部分.您不一定必须使用 lambda,但这通常是最方便的.

                So no, you don't just add a single line like you do an attribute; you need to explicitly wrap the section of your test that will throw the exception, in a call to Assert.Throws. You don't necessarily have to use a lambda, but that's often the most convenient.

                这篇关于NUnit 预期异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 MFC No CLR 中访问 Winform ActiveX 控件 下一篇:CI:Hudson with .Net vs CruiseControl.Net

                相关文章

                  <legend id='wuQO4'><style id='wuQO4'><dir id='wuQO4'><q id='wuQO4'></q></dir></style></legend><tfoot id='wuQO4'></tfoot>

                1. <small id='wuQO4'></small><noframes id='wuQO4'>

                    <bdo id='wuQO4'></bdo><ul id='wuQO4'></ul>
                  <i id='wuQO4'><tr id='wuQO4'><dt id='wuQO4'><q id='wuQO4'><span id='wuQO4'><b id='wuQO4'><form id='wuQO4'><ins id='wuQO4'></ins><ul id='wuQO4'></ul><sub id='wuQO4'></sub></form><legend id='wuQO4'></legend><bdo id='wuQO4'><pre id='wuQO4'><center id='wuQO4'></center></pre></bdo></b><th id='wuQO4'></th></span></q></dt></tr></i><div id='wuQO4'><tfoot id='wuQO4'></tfoot><dl id='wuQO4'><fieldset id='wuQO4'></fieldset></dl></div>