【发布时间】:2011-11-22 02:27:16
【问题描述】:
我正在编写一个异步单元测试,我想使用 lambdas(或匿名方法?)将它串在一起,因此我不必为延续定义命名函数。
我已经阅读了几篇关于 lambdas 的帖子,但是 most 的 these 处理我不感兴趣的每种样式构造。
我想做类似以下的事情(取自here):
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
{
[TestClass]
public class Test2 : SilverlightTest
{
[TestMethod]
[Asynchronous]
public void TestAsync1()
{
var eventRaised = false;
var result = false;
var timer = new System.Windows.Threading.DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(2);
timer.Tick += (object sender, EventArgs e) =>
{
timer.Stop();
// Simulate an expected result
result = true;
// Mark the event has being raised
eventRaised = true;
};
// Start timer
EnqueueCallback(() => timer.Start());
// Wait until event is raised
EnqueueConditional(() => eventRaised);
EnqueueCallback(() =>
{
// Additional tasks can be added here
Assert.IsTrue(result);
});
EnqueueTestComplete();
}
}
}
但我想我不需要 EnqueueCallback() 的东西。
以下是我没有 lambdas 的代码:
[TestClass]
public class IdentityEditDatabaseTest : WorkItemTest
{
[TestMethod, Asynchronous] public void testNullInsert()
{
wipeTestData(testNullInsertContinue1);
}
private void testNullInsertContinue1(String errorString)
{
IdentityProperties properties = new IdentityProperties(getContext());
properties.setUserName(DATABASE_TEST);
postUserEdit(properties, testNullInsertContinue2);
}
private void testNullInsertContinue2(String errorString)
{
Assert.assertTrue(errorString == null);
wipeTestData(testNullInsertContinue3);
}
private void testNullInsertContinue3(String errorString)
{
TestComplete();
}
}
...
再次,问题是:
如何使用 lambda(或匿名方法?)将上述内容串在一起,这样我就不必为延续定义命名函数?
请尽可能多地解释新语法,因为我仍在努力理解这个概念。
非常感谢!
【问题讨论】:
-
我可能完全是愚蠢的,也许我没有理解这个问题(看起来很难),但是你为什么不订阅 timer.Tick 事件呢?