DUnit - class MyTest { mixin TestMixin; void testMethod1() {} void testMethod2() {}}

linkrope linkrope at github.com
Thu Aug 30 12:42:30 PDT 2012


Add some variable to the test case: bool finished;
Set the variable in the callback: finished = true;
Add something like

     assertWithTimeout({return finished;});

at the end of the test case, following ev.run;

Does it help?

On Wednesday, 29 August 2012 at 19:20:25 UTC, Shripad K wrote:
> How do I test callbacks/delegates which are not triggered 
> immediately? Especially when I need to do unit tests with an 
> event loop?
> A simple example from my codebase (SaaSy here is a custom HTTP 
> client to an API endpoint):
>
> class TestSaaSy {
>     mixin TestMixin;
>
>     // this works
>     void test_encoded_auth() {
>         auto ev = new EventLoop;
>         auto saasy = new SaaSy(ev, "test", "test");
>         assert(saasy.encoded_auth == "dGVzdDp0ZXN0dGVzdA==", 
> "encoding issue");
>         ev.close;
>         ev.run;
>     }
>
>     // won't work. test gets finished even before the callback 
> is fired!
>     void test_get_subscription() {
>         auto ev = new EventLoop;
>         auto saasy = new SaaSy(ev, "test", "test");
>         saasy.getSubscription("ref363466", (bool err, string 
> response) {
>             assert(err == true);
>             ev.close;
>         });
>         ev.run;
>     }
> }


More information about the Digitalmars-d-announce mailing list