将具有2个参数的函数转换为lambda动作
问题描述:
我想测试(使用using Microsoft.VisualStudio.TestTools.UnitTesting
)此测试函数的顶部行导致引发DataMisalignedException
。将具有2个参数的函数转换为lambda动作
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
作为一个行动,在两个参数传递从测试的顶线?我的尝试是在上面的代码中,但没有传递这两个参数。
答
为了表明测试方法执行过程中预计会发生异常,您可以使用测试方法顶部的[ExpectedException]
属性。
[TestClass]
public class JsonSchemaValidatorTests
{
[TestMethod]
[ExpectedException(typeof(DataMisalignedException))]
public void ShouldThrowOnBadPhoneNumber()
{
JsonSchemaValidator.validateAgainstJsonSchema(ProviderService.getErronousProviders(), "./provider-schema.json");
}
}
我想你想'Assert.ThrowsException(()=> JsonSchemaValidator.validateAgainstJsonSchema(ProviderService.getErronousProviders(), “./provider-schema.json”))',但我不能找到'Assert.ThrowsException'的文档我通常只使用测试方法中的'ExpectedException'属性。 –
juharr
@juharr是的谢谢,这就是我正在寻找。我发现它不是在转换函数,而是在匿名函数中调用它。欢呼 – BeniaminoBaggins
我的猜测是'Assert.ThrowsException'需要一个'Action'而不是'Action'。 –
juharr