NUnit的3.0和Assert.Throws [英] NUnit 3.0 and Assert.Throws

查看:135
本文介绍了NUnit的3.0和Assert.Throws的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一些单元测试与NUnit的3.0,不像V2.X,的ExpectedException()从库中已被删除。

I am writing some unit tests with NUnit 3.0 and, unlike v2.x, ExpectedException() has been removed from the library.

根据答案,我可以肯定地看到在试图专门捕获凡在测试人们希望他们的系统抛出异常(而不只是说在任何地方考试)的逻辑。

Based on this answer, I can definitely see the logic in trying to catch specifically where in the test one expects their system to throw an exception (rather than just saying 'anywhere in the test').

不过,我倾向于对我的安排,法和断言步骤非常明确的,这使得它的一个挑战。

However, I tend to be very explicit about my Arrange, Act, and Assert steps and this makes it a challenge.

我以前做是这样的:

[Test, ExpectedException(typeof(FormatException))]
public void Should_not_convert_from_prinergy_date_time_sample1()
{
    //Arrange
    string testDate = "20121123120122";

    //Act
    testDate.FromPrinergyDateTime();

    //Assert
    Assert.Fail("FromPrinergyDateTime should throw an exception parsing invalid input.");
}

现在我需要做的是这样的:

Now I need to do something like:

[Test]
public void Should_not_convert_from_prinergy_date_time_sample2()
{
    //Arrange
    string testDate = "20121123120122";

    //Act/Assert
    Assert.Throws<FormatException>(() => testDate.FromPrinergyDateTime());
}

这并不可怕,但muddies该法案,并断言,在我看来, 。 (显然,这个简单的测试,不难跟随,但可能会在更大的测试更具挑战性)。

This isn't terrible, but muddies the Act and Assert, in my opinion. (Obviously, for this simple test, it's not hard to follow, but might be more challenging in larger tests).

我有一个同事建议我摆脱 Assert.Throws 干脆,只是这样做:

I've had a colleague suggest I get rid of Assert.Throws altogether and just do something like:

[Test]
public void Should_not_convert_from_prinergy_date_time_sample3()
{
    //Arrange
    int exceptions = 0;
    string testDate = "20121123120122";

    //Act
    try
    {
        testDate.FromPrinergyDateTime();
    }
    catch (FormatException) { exceptions++;}

    //Assert
    Assert.AreEqual(1, exceptions);
}

下面,我坚持严格的AAA格式,但在连代价。更多膨胀

Here, I stick with the strict AAA format, but at the expense of even more bloat.

所以我的问题出去AAA式测试:你会怎么做某种异常的验证测试的样,我想在这里做。

So my question goes out to AAA-style testers: How would you do some sort of exception validation testing like I am trying to do here?

推荐答案

我看到你从哪里来的,尽管我不介意结合法/断言在这种情况下步骤。

I see where you're coming from, even though I don't mind combining Act/Assert steps in this case.

我能想到的唯一的事情就是(这里 FromPrinergyDateTime )存储实际委托到一个变量行为的步骤,然后断言的:

The only thing I can think of is to store the actual delegate (here to FromPrinergyDateTime) into a variable as the "act" step and then assert it:

[Test]
public void Should_not_convert_from_prinergy_date_time_sample2()
{
    //Arrange
    string testDate = "20121123120122";

    //Act
    ActualValueDelegate<object> testDelegate = () => testDate.FromPrinergyDateTime();

    //Assert
    Assert.That(testDelegate, Throws.TypeOf<FormatException>());
}



我得到的行为步骤不是的真的的演技,而是定义动作是什么。但是,它清楚地界定正在测试什么样的行动。

I get that the "act" step isn't really acting, but rather defining what the action is. However, it does clearly delineate what action is being tested.

这篇关于NUnit的3.0和Assert.Throws的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆