如何根据Arrange-Act-Assert范例处理nUnit 3中的多个异常测试? [英] How to handle multiple exception testing in nUnit 3 according to Arrange-Act-Assert paradigm?

查看:112
本文介绍了如何根据Arrange-Act-Assert范例处理nUnit 3中的多个异常测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在nUnit 3中,进行了相当多的更改,其中 ExpectedException 已被删除,由断言模型代替.

In nUnit 3, there's been considerable changes made, among which the ExpectedException has been dropped to be replaced by the assertion model.

我找到了此方法,如下所示.

I've found this and this example but the recommendation is to use Assert.That() instead so I want to use this approach as shown below.

//Arrange
string data = "abcd";
//Act
ActualValueDelegate<object> test = () => data.MethodCall();
//Assert
Assert.That(test, Throws.TypeOf<ExceptionalException>());

但是,我想测试多个不同参数的断言,但是我似乎无法使其工作.我尝试了以下方法.

However, I'd like to test the assert for a number of different parameters but I can't seem to get it to work. I've tried the following.

List<List<Stuff>> sets = new[] { 0, 1, 2 }
  .Select(_ => Enumerable.Repeat(new Stuff(_), _).ToList())
  .ToList();
Assert.Throws(() => Transform.ToThings(new List<Stuff>()));

上面的方法可以根据需要创建列表列表,并测试 Transform.Stuff()调用.但是,我还没有找到集合插入事物的方法.

The above works as to creating the list of lists, as desired and testing the Transform.Stuff() call. However, I haven't figured out a way to plug sets into things.

有可能吗?例子在哪里? (大多数搜索都淹没在nUnit 2.x和官方文档网站上,但没有任何帮助.)

Is it possible? Where are the examples? (Most of the searches drown in nUnit 2.x and on the official documentation site, I see nothing that helps me.)

推荐答案

您应该使用

You should look into using the TestCaseSource attribute to define the inputs to your method:

[TestCaseSource("StuffSets")]
public void ToThings_Always_ThrowsAnException(List<Stuff> set)
{
    // Arrange
    // Whatever you need to do here...

    // Act
    ActualValueDelegate<object> test = () => Transform.ToThings(set);

    // Assert
    Assert.That(test, Throws.TypeOf<SomeKindOfException>());
}

public static IEnumerable<List<Stuff>> StuffSets = 
{
    get 
    {
        return new[] { 0, 1, 2 }
            .Select(_ => Enumerable.Repeat(new Stuff(_), _).ToList())
            .ToList();
    }
};

这将从StuffSets返回的每个List<Stuff>调用一次ToThings_Always_ThrowsAnException(在此为三次).

This will invoke ToThings_Always_ThrowsAnException once for each List<Stuff> that is returned from StuffSets (here, three times).

这篇关于如何根据Arrange-Act-Assert范例处理nUnit 3中的多个异常测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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