Testing Coroutines
Here's a few simple steps to test a method that utilizes coroutines:
- When testing a coroutine, simply enumerate each result, one at a time. Do not call execute on your results. Remember, you are not testing the IResult implementation, but the method which uses it.
- Check to make sure that the correct IResult is yielded and check any properties it has which would indicate it's correct/incorrect configuration.
- Set any properties on the IResult which would have been set as a result of execution....but do not call execute.
- Go to beginning.
By following these steps you will be able to "ignore" the async part of the coroutine (if it has one) by exerting direct control over the enumeration. You can even choose to stop enumerating at any point once you have confirmed that your test passes/fails. You will probably want to use several test methods to test a single coroutine implementation in order to keep individual tests simple and small.
Here's a sample class you might use to help in enumerating the results in tests:
publicclass CoroutineEnumerator { privatereadonly IEnumerator<IResult> enumerator; public CoroutineEnumerator(IEnumerable<IResult> enumerable) { this.enumerator = enumerable.GetEnumerator(); } public CoroutineEnumerator(IEnumerator<IResult> enumerator) { this.enumerator = enumerator; } public TCoroutine Next<TCoroutine>() { while(enumerator.MoveNext()) { if(enumerator.Current is TCoroutine) return (TCoroutine)enumerator.Current; } thrownew InvalidOperationException("List of coroutines does not include " + typeof(TCoroutine).Name); } publicvoid Finish() { while(enumerator.MoveNext()) {} } }