use*_*550 3 silverlight unit-testing silverlight-toolkit silverlight-5.0
我遇到了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, _password, response =>
{
Assert.IsTrue(response);
autoResetEvent.Set();
});
if (!autoResetEvent.WaitOne(Constants.Timeout))
{
Assert.Fail("Test timed out.");
}
}
Run Code Online (Sandbox Code Playgroud)
编辑: 我的最终解决方案是对Vladmir解决方案的修改:
[TestMethod]
[Asynchronous]
public void AuthShouldSucceed()
{
var complete = false;
var result = false;
_authRepository.Authenticate(_username, _password, response =>
{
complete = true;
result = response;
});
EnqueueConditional(() => complete);
EnqueueCallback(() => Assert.IsTrue(result));
EnqueueTestComplete();
}
Run Code Online (Sandbox Code Playgroud)
Vla*_*hov 12
如果您正在使用Silverlight Unit Tests Framework,请尝试下一步重写您的测试:
[TestMethod]
[Asynchronous]
public void AuthShouldSucceed()
{
var done = false;
var authResult = false;
_authRepository.Authenticate(_username, _password, response =>
{
var done = true;
authResult = response;
});
EnqueueConditional(() => done);
EnqueueCallback(() => Assert.IsTrue(authResult));
EnqueueTestComplete();
}
Run Code Online (Sandbox Code Playgroud)
您的测试类应该派生自SilverlightTest类:
[TestClass]
public class MyTests: SilverlightTest
Run Code Online (Sandbox Code Playgroud)