MonoTouch线程测试

时间:2013-05-15 05:06:16

标签: .net testing xamarin.ios xamarin-studio

我有一个在不同线程中执行某些功能的类。我想在我的MonoTouch应用程序中测试这个类。所以我在测试项目中添加了一个测试夹具。我发现MonoTouch不会等待测试结束,它只是说测试用例仍在运行时它是“成功的”。案例如下:

[Test]
public void ThreadTest()
{
    Timer ApplicationTimer = new Timer {Enabled = true, Interval = 2500};
    ApplicationTimer.Elapsed += ApplicationTimer_Tick;
}

private void ApplicationTimer_Tick (object sender, ElapsedEventArgs e)
{
   Assert.Fail("failing"); // by the time the debugger hits this point, the UI already says that all tests passed. which is totally wrong. 
}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

这不是MonoTouch特定的问题 - 该测试在所有测试人员中都会失败。

等待此异步事件的测试可能如下所示:

        private ManualResetEvent _mre = new ManualResetEvent(false);

        [Test]
        public void ThreadTest()
        {
            Timer ApplicationTimer = new Timer {Enabled = true, Interval = 2500};
            ApplicationTimer.Elapsed += ApplicationTimer_Tick;
            if (!_mre.WaitOne(3000))
            {
                Assert.Fail("Timer was not signalled");
            }
        }

        private void ApplicationTimer_Tick (object sender, ElapsedEventArgs e)
        {
            _mre.Set();             
        }

但是你必须非常小心地编写这种测试,以确保你没有锁定线程,跨测试重用对象等。

相关问题