我有一个带有4个字符串参数的方法。这些参数中的每一个都存在一个空参数验证保护。有没有一种方法可以编写单个xUnit null验证单元测试来通用地测试所有4个参数?
坦率地说,这似乎不是一件很有用的事情。:-(然而。。。
在NUnit中,您可以执行类似以下的操作
[TestCase(null, "b", "c", "d")]
[TestCase("a", null, "c", "d")]
[TestCase("a", "b", null, "d")]
[TestCase("a", "b", "c", null)]
public void NullArgDetected(string a, string b, string c, string d)
{
// call method and assert that it throws
}
当然,您必须更改参数的类型以匹配您正在调用的方法。如果C#不允许将该类型作为Attribute构造函数参数,则需要使用[TestCaseSource]
而不是[TestCase
]。
就我个人而言,如果我有10个论点,我可能会这么做。对于第四个,我只想写四个单独的测试方法。