【发布时间】:2017-02-13 19:46:46
【问题描述】:
我想测试(使用using Microsoft.VisualStudio.TestTools.UnitTesting)这个测试函数的第一行会导致DataMisalignedException 被抛出。
namespace xxx.Services.SupplierApiTests
{
[TestClass]
public class JsonSchemaValidatorTests
{
[TestMethod]
public void ShouldThrowOnBadPhoneNumber()
{
JsonSchemaValidator.validateAgainstJsonSchema(ProviderService.getErronousProviders(), "./provider-schema.json");
Action<IList, string> myAction = (x, y) => JsonSchemaValidator.validateAgainstJsonSchema(x, y);
Assert.ThrowsException<DataMisalignedException>(myAction);
}
}
}
我如何使用JsonSchemaValidator.validateAgainstJsonSchema 作为一个动作,从测试的第一行传入两个参数?我的尝试是在上面的代码中,但没有传递两个参数。
【问题讨论】:
-
我想你想要
Assert.ThrowsException<DataMisalignedException>(() => JsonSchemaValidator.validateAgainstJsonSchema(ProviderService.getErronousProviders(), "./provider-schema.json")),但我找不到Assert.ThrowsException的文档我自己通常只在测试方法上使用ExpectedException属性。 -
@juharr 是的,谢谢,这就是我想要的。我看到它没有转换函数,而是在匿名函数中调用它。干杯
-
我的猜测是
Assert.ThrowsException需要Action而不是Action<IList, string>。 -
ThrowsException()也可能希望将参数列表传递给操作。但我在 MSDN 中找不到该方法的参考...