Silverlight异步单元测试

Silverlight异步单元测试,第1张

概述我遇到了Silverlight Unit Test Framework的一个奇怪问题.每次执行的第一个方法都失败了.我用完全相同的代码进行了第二次测试,然后通过.第一次调用它的奇怪之处在于它实际上等待超时然后执行存储库调用(如果你关心的话,它下面是一个HTTP PUT).这是代码 – 第一个代码每次都失败,第二个代码每次都会失败: [TestMethod] public void Auth 我遇到了Silverlight Unit Test Framework的一个奇怪问题.每次执行的第一个方法都失败了.我用完全相同的代码进行了第二次测试,然后通过.第一次调用它的奇怪之处在于它实际上等待超时然后执行存储库调用(如果你关心的话,它下面是一个http PUT).这是代码 – 第一个代码每次都失败,第二个代码每次都会失败:

[TestMethod]    public voID AuthShouldSucceed()    {        var autoresetEvent = new autoresetEvent(false);        _authRepository.Authenticate(_username,_password,response =>        {            Assert.IsTrue(response);            autoresetEvent.Set();        });        if (!autoresetEvent.WaitOne(Constants.Timeout))        {            Assert.Fail("Test timed out.");        }     }    [TestMethod]    public voID AuthShouldSucceed2()    {        var autoresetEvent = new autoresetEvent(false);        _authRepository.Authenticate(_username,response =>        {            Assert.IsTrue(response);            autoresetEvent.Set();        });        if (!autoresetEvent.WaitOne(Constants.Timeout))        {            Assert.Fail("Test timed out.");        }     }

编辑:
我的最终解决方案是对Vladmir解决方案的修改:

[TestMethod]    [Asynchronous]    public voID AuthShouldSucceed()    {        var complete = false;        var result = false;        _authRepository.Authenticate(_username,response =>        {            complete = true;            result = response;        });        EnqueueConditional(() => complete);        EnqueueCallback(() => Assert.IsTrue(result));        EnqueueTestComplete();    }
解决方法 如果您正在使用Silverlight Unit Tests Framework,请尝试下一步重写您的测试:

[TestMethod]    [Asynchronous]    public voID AuthShouldSucceed()    {        var done = false;        var authResult = false;        _authRepository.Authenticate(_username,response =>        {            var done = true;            authResult = response;        });        EnqueueConditional(() => done);        EnqueueCallback(() => Assert.IsTrue(authResult));        EnqueueTestComplete();    }

您的测试类应该派生自SilverlightTest类:

[TestClass]public class MyTests: SilverlightTest
总结

以上是内存溢出为你收集整理的Silverlight异步单元测试全部内容,希望文章能够帮你解决Silverlight异步单元测试所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1007391.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存