How can i execute the DOH test synchronously?

285 views Asked by At

Can anybody help me in finding a solution for this problem. I have (assume) 3 doh functions 1st one is async and the rest are synchronous. I have to make async function to be called first and the result of this function to be passed to other two functions is it possible?

Example :

doh.register(".....", [
{
 name : "asyncFunction",
 runTest : function(){
  function callback(result){
    //How to pass the result to fun_2 and fun_3 
    //also fun_2 or fun_3 should be deferred until this function executes
  }
 }
},
function fun_2(result){
 //doh.assertTrue(.....);
},
function fun_3(result){
//doh.assertTrue(.....);
}

Any help would be great.

1

There are 1 answers

1
Royston Shufflebotham On

So, it sounds like your first function is basically a setup function for the other tests.

It is possible to do - basically using Deferreds/promises, but it's a little bit icky, and you might get badly stung by test timeouts.

So, here's something that does a setup with a bit of asynchronous code that takes 2s. All the tests become asynchronous tests that do their work once the 'setup' Deferred has completed.

Because your 'follow on' tests have become asynchronous, you need to make sure that their timeouts cope with the time that'll be taken by your asynchronous setup (at least for the first one that happens to run).

            // Some asynchronous initialization that takes 2s
            setTimeout(function() {
                setupCompletion.resolve({ result: 42 });
            }, 2000);

            doh.register("my.test1", [
                {
                    name: "waits for async setup to complete",
                    timeout: 5000,
                    runTest: function() {
                        var d = new doh.Deferred();
                        setupCompletion.then(function (res) {
                            doh.is(42, res.result);
                            d.callback(true);
                        });
                        return d;
                    }
                },
                {
                    name: "also waits for async setup to complete",
                    timeout: 5000,
                    runTest: function() {
                        var d = new doh.Deferred();
                        setupCompletion.then(function (res) {
                            doh.is(43, res.result + 1);
                            d.callback(true);
                        });
                        return d;
                    }
                }
            ]);

Of course, it would be nice if it were possible to arrange for a test's setUp function to return a Deferred, but doh doesn't support that right now (as of v1.7.2).