如何创建接受 lambda 表达式的方法
所以我发现自己一直在编写这段代码:
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你快到了。测试助手应该如下所示:
调用它:
注意
() => 的用法some
这意味着 lambda 没有参数。您还必须指定ArgumentNullException
的泛型参数,因为编译器无法推断它。You're almost there. Here's what the test helper should look like:
And to call it:
Note the usage of
() => something
which means the lambda has no parameters. You also have to specify the generic argument ofArgumentNullException
because the compiler cannot infer it.以下内容应该可以满足您的需要(我添加了
TException
的类型参数和theAction
的调用)。您可以使用以下代码来调用它:
您需要指定类型参数来指示要测试哪种类型的异常。
() =>; ...
语法是一个不带参数的 lambda 表达式。The following should do what you need (I've added the type parameter for
TException
and the invocation oftheAction
).You can call it with the following code:
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.