如何创建接受 lambda 表达式的方法

发布于 2024-08-18 08:22:43 字数 1203 浏览 3 评论 0原文

所以我发现自己一直在编写这段代码:


[TestMethod]
[Description("Asserts that an ArgumentNullException is thrown if ResetPassword(null) is called")]
public void ResetPassword_Throws_ArgumentNullException_With_Null_Parameter( )
{
    try
    {
        new MembershipServiceProvider( ).ResetPassword( null );
    }
    catch ( ArgumentNullException )
    {
        // ArgumentNullException was expected
        Assert.IsTrue( true );
    }
    catch
    {
        Assert.Fail( "ArgumentNullException was expected" );
    }
}

所以我不想一遍又一遍地编写这段代码,而是创建一个接受 Lambda 表达式的方法,该表达式将在 try 块中执行该方法。

像这样的事情:


public void AssertExpectedException( Action theAction ) where TException : Exception
{
    try
    {
        // Execute the method here
    }
    catch ( TException )
    {
        Assert.IsTrue( true );
    }
    catch
    {
        Assert.Fail( string.Format( "An exception of type {0} was expected", typeof( TException ) ) );
    }
}

所以我可以做这样的事情:


var provider = new MembershipServiceProvider();
AssertExpectedException(provider => provider.ResetPassword(null));

我真的不确定这是否在正确的轨道上,但希望有人能指出我正确的方向。

谢谢

So I find myself writing this code all the time:


[TestMethod]
[Description("Asserts that an ArgumentNullException is thrown if ResetPassword(null) is called")]
public void ResetPassword_Throws_ArgumentNullException_With_Null_Parameter( )
{
    try
    {
        new MembershipServiceProvider( ).ResetPassword( null );
    }
    catch ( ArgumentNullException )
    {
        // ArgumentNullException was expected
        Assert.IsTrue( true );
    }
    catch
    {
        Assert.Fail( "ArgumentNullException was expected" );
    }
}

So instead of writing this code over and over I'd really like to create a method which accepts a Lambda expression which will execute the method in the try block.

Something like this:


public void AssertExpectedException( Action theAction ) where TException : Exception
{
    try
    {
        // Execute the method here
    }
    catch ( TException )
    {
        Assert.IsTrue( true );
    }
    catch
    {
        Assert.Fail( string.Format( "An exception of type {0} was expected", typeof( TException ) ) );
    }
}

So I can do something like this:


var provider = new MembershipServiceProvider();
AssertExpectedException(provider => provider.ResetPassword(null));

I'm really not sure if any of this is on the right track but hopefully someone can point me in the right direction.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法回应 2024-08-25 08:22:43

你快到了。测试助手应该如下所示:

public void AssertExpectedException<TException>( Action theAction )
    where TException : Exception 
{ 
    try 
    { 
        // Execute the method here 
        theAction();
    } 
    catch ( TException ) 
    { 
        // The Assert here is not necessary
    } 
    catch 
    { 
        Assert.Fail( string.Format(
            "An exception of type {0} was expected",
            typeof(TException))); 
    } 
} 

调用它:

var provider = new MembershipServiceProvider(); 
AssertExpectedException<ArgumentNullException>(() => provider.ResetPassword(null)); 

注意 () => 的用法some 这意味着 lambda 没有参数。您还必须指定 ArgumentNullException 的泛型参数,因为编译器无法推断它。

You're almost there. Here's what the test helper should look like:

public void AssertExpectedException<TException>( Action theAction )
    where TException : Exception 
{ 
    try 
    { 
        // Execute the method here 
        theAction();
    } 
    catch ( TException ) 
    { 
        // The Assert here is not necessary
    } 
    catch 
    { 
        Assert.Fail( string.Format(
            "An exception of type {0} was expected",
            typeof(TException))); 
    } 
} 

And to call it:

var provider = new MembershipServiceProvider(); 
AssertExpectedException<ArgumentNullException>(() => provider.ResetPassword(null)); 

Note the usage of () => something which means the lambda has no parameters. You also have to specify the generic argument of ArgumentNullException because the compiler cannot infer it.

小草泠泠 2024-08-25 08:22:43

以下内容应该可以满足您的需要(我添加了 TException 的类型参数和 theAction 的调用)。

public void AssertExpectedException<TException>(Action theAction) 
    where TException : Exception
{
    try
    {
        theAction();
    }
    catch (TException)
    {
        Assert.IsTrue(true);
    }
    catch
    {
        Assert.Fail(string.Format("An exception of type {0} was expected", 
            typeof(TException)));
    }
}

您可以使用以下代码来调用它:

var provider = new MembershipServiceProvider();
AssertExpectedException<ArgumentNullException>(() => provider.ResetPassword(null));

您需要指定类型参数来指示要测试哪种类型的异常。 () =>; ... 语法是一个不带参数的 lambda 表达式。

The following should do what you need (I've added the type parameter for TException and the invocation of theAction).

public void AssertExpectedException<TException>(Action theAction) 
    where TException : Exception
{
    try
    {
        theAction();
    }
    catch (TException)
    {
        Assert.IsTrue(true);
    }
    catch
    {
        Assert.Fail(string.Format("An exception of type {0} was expected", 
            typeof(TException)));
    }
}

You can call it with the following code:

var provider = new MembershipServiceProvider();
AssertExpectedException<ArgumentNullException>(() => provider.ResetPassword(null));

You need to specify the type argument to indicate which type of exception to test for. The () => ... syntax is a lambda expression that takes no parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文