是否可以创建NUnit Test方法来检查方法是否返回预期的数据类型?
这就是我的意思:
我有一个静态字符串,它接受两个参数并检查它是否与另一个字符串匹配.如果是,方法只返回该字符串.
我想测试以确保此方法确实返回字符串类型和可能发生的任何异常.
示例代码:
public static string GetXmlAttributeValue(this XmlElement element, string attributeName)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
if (attributeName == null)
{
throw new ArgumentNullException("attributeName");
}
string attributeValue = string.Empty;
if (element.HasAttribute(attributeName))
attributeValue = element.Attributes[attributeName].Value;
else
throw new XmlException(element.LocalName + " does not have an attribute called " + attributeName);
return attributeValue;
}
Run Code Online (Sandbox Code Playgroud)
以下是我的解决方案的样子:
我想在TestLibrary类库中编写测试代码.
通常,不需要测试返回类型.C#是静态类型语言,因此此方法不能返回与string不同的其他内容.
但是如果你想编写一个测试,如果有人更改了返回类型,测试会失败,你可以这样做:
Assert.That(result, Is.TypeOf<string>());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7385 次 |
| 最近记录: |