gpt4 book ai didi

.net - 线程的 MonoTouch 测试

转载 作者:行者123 更新时间:2023-11-28 20:06:24 24 4
gpt4 key购买 nike

我有一个类在不同的线程中执行某些功能。我想在我的 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.
}

如有任何帮助,我们将不胜感激。

最佳答案

这不是 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();
}

但是您在编写此类测试时必须非常小心,以确保您没有锁定线程、跨测试重用对象等。

关于.net - 线程的 MonoTouch 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16557280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com