I will start today with the new special assertion Retry.Until
. It is special because it is not of the usual form Assert.Something
.
Retry.Until
lets you evaluate a specific condition several times until it becomes true, or until some timeout mechanism triggers an assertion failure.
The assertion comes with a nice fluent syntax which allows some flexible configuration. It works a little bit like the famous Rhino.Mocks expectations. You can specify the number of times the condition must be evaluated (Times
), a polling time between each evaluation (WithPolling
), a global timeout duration for the entire operation (WithTimeout
), or some custom action to execute between each cycle (DoBetween
). Of course, you can finally specify the condition to evaluate (Until
). So far, 3 kinds of conditions are accepted:
- A
WaitHandle
instance which is expected to be signaled. - A
Thread
instance which is expected to be terminated. - A versatile predicate
Func<bool>
which is expected to return true.
[TestFixture]
public class MyTestFixture
{
[Test]
public void MyTestMethod()
{
var foo = new Foo();
foo.RunSomeAsyncOperation();
Retry.Times(5)
.WithPolling(TimeSpan.FromSeconds(1))
.Until(() => Foo.HasTerminated());
}
}