Асинхронное юнит-тестирование Silverlight
У меня странная проблема с платформой Silverlight Unit Test Framework. Самый первый выполняемый метод каждый раз дает сбой. У меня есть второй тест с точно таким же кодом, и он проходит. Странная вещь в первый раз, когда он вызывается, заключается в том, что он на самом деле ожидает тайм-аутand then выполняет вызов хранилища (под ним 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.");
}
}
Edit: Мое окончательное решение - это модификация решения Владмира:
[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();
}