Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将具有2个参数的函数转换为lambda操作_C#_Lambda_Action_Assert - Fatal编程技术网

C# 将具有2个参数的函数转换为lambda操作

C# 将具有2个参数的函数转换为lambda操作,c#,lambda,action,assert,C#,Lambda,Action,Assert,我想测试(使用Microsoft.VisualStudio.TestTools.UnitTesting)此测试函数的顶行是否导致引发DataMilledException namespace xxx.Services.SupplierApiTests { [TestClass] public class JsonSchemaValidatorTests { [TestMethod] public void ShouldThrowOnBadP

我想测试(使用Microsoft.VisualStudio.TestTools.UnitTesting)此测试函数的顶行是否导致引发
DataMilledException

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);
        }
    }
}
namespace xxx.Services.supplierapiests
{
[测试类]
公共类JSONSCHEMAVALIDATORTEST
{
[测试方法]
public void ShouldThrowOnBadPhoneNumber()
{
validateAgainstJsonSchema(ProviderService.getErrorNousProviders(),“/provider schema.json”);
Action myAction=(x,y)=>JsonSchemaValidator.validategainstjsonschema(x,y);
Assert.ThrowsException(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.validategainstjsonschema(ProviderService.geterronosproviders(),“/provider schema.json”))
但我找不到断言的文档。通过异常我通常只在测试方法上使用
ExpectedException
属性。@juharr是的,谢谢,这就是我要找的。我看到它不是在转换函数,而是在匿名函数中调用它。我的猜测是,
Assert.ThrowsException
需要一个
操作
,而不是
操作
ThrowsException()
也可能需要一个参数列表传递给该操作。但我在MSDN中找不到该方法的参考。。。