Loading ...
Sorry, an error occurred while loading the content.

YUI Test, unit test for an asynchronous API

Expand Messages
  • Joe C
    Hello, I would like advice on a good strategy for a unit test on an API that is asynchronous. I want to verify the results of an API takes in a callback
    Message 1 of 4 , Nov 14, 2007
    • 0 Attachment
      Hello,

      I would like advice on a good strategy for a unit test on an API that is asynchronous. I want to verify the results of an API takes in a callback function that will fire at a later time. I don't want to have to use polling either - I'd prefer something more elegant.

      Here is what I'm doing now:

      testFoo : function () {
            myfunction(...,mycallback); //  mycallback fires asynchronously 
      }

      mycallback: function(response) {
          var Assert = YAHOO.util.Assert ;
          Assert.areEqual(...);
      }


      This design doesn't work through because the Assert is called from mycallback and not a test* function.

      I get an error in firebug:
      "uncaught exception: ComparisonFailure: Values should be equal. Expected: false (boolean) Actual:true (boolean)"

      This error is expected because the YUI Test infrastructure is designed to catch these exceptions from the test* functions.

      Thanks!

      -Joe
    • Nicholas Zakas
      Hi Joe, Asynchronous testing is not possible in the 2.3 version of YUI Test. However, it will be available in the 2.4 version via the wait() method. It s a
      Message 2 of 4 , Nov 26, 2007
      • 0 Attachment

        Hi Joe,

         

        Asynchronous testing is not possible in the 2.3 version of YUI Test. However, it will be available in the 2.4 version via the wait() method. It’s a little bit difficult to tell what you’re trying to accomplish from your code, but in 2.4 you’ll be able to do something like this:

         

        testFoo : function(){

            this.wait(function(){

                YAHOO.util.Assert.areEqual(…);

         

            }, 5000);   //wait 5 seconds

        }

         

        If you can hold on until 2.4, this job will become much easier for you. J

         

        -Nicholas

         


        From: ydn-javascript@yahoogroups.com [mailto: ydn-javascript@yahoogroups.com ] On Behalf Of Joe C
        Sent: Wednesday, November 14, 2007 6:16 AM
        To: ydn-javascript@yahoogroups.com
        Subject: [ydn-javascript] YUI Test, unit test for an asynchronous API

         

        Hello,

        I would like advice on a good strategy for a unit test on an API that is asynchronous. I want to verify the results of an API takes in a callback function that will fire at a later time. I don't want to have to use polling either - I'd prefer something more elegant.

        Here is what I'm doing now:

        testFoo : function () {
              myfunction(. ..,mycallback) ; //  mycallback fires asynchronously 
        }

        mycallback: function(response) {
            var Assert = YAHOO.util.Assert ;
            Assert.areEqual( ...);
        }


        This design doesn't work through because the Assert is called from mycallback and not a test* function.

        I get an error in firebug:
        "uncaught exception: ComparisonFailure: Values should be equal. Expected: false (boolean) Actual:true (boolean)"

        This error is expected because the YUI Test infrastructure is designed to catch these exceptions from the test* functions.

        Thanks!

        -Joe

      • Nicholas Zakas
        Hi Joe, There s actually two ways to use the new wait() method. The first is to use it like setTimeout(), as I described. The second is to pass in no arguments
        Message 3 of 4 , Nov 29, 2007
        • 0 Attachment

          Hi Joe,

           

          There’s actually two ways to use the new wait() method. The first is to use it like setTimeout(), as I described. The second is to pass in no arguments at all, which causes the entire test process to stop. You can then resume the process by calling resume(). In your case, this may be more reasonable. Still, from your description, this may be a bit tricky to test. I think something like the following would work:

           

          testMyFunction: function () {

           

              var that = this;

              myfunction(inputdata, function(outputData){

           

                 //restart the test

                 that.resume(function(){

                      YAHOO.util.Assert.areEqual(5, outputData.value);

                 });

              });

           

              //wait until resume() is called

              this.wait();

          }

           

          This should work to test your function assuming it behaves the way I think it does (using setTimeout() to call the callback function?). It may be a little trickier to determine if the callback function was ever called. If you’re using setTimeout(), you are virtually guaranteed that the function will be called, so is that a major concern for you?

           

          -Nicholas

           

           


          From: Joe C [mailto:joe.jconti@...]
          Sent: Thursday, November 29, 2007 11:39 AM
          To: Nicholas Zakas
          Subject: Re: [ydn-javascript] YUI Test, unit test for an asynchronous API

           

          Hi Nicholas,

          Thanks for responding. I'm not sure adding wait(...) time to YUI Test framework will help me. Here is a little more information on what I'm trying to do.

          The function I want to test has an API that looks like this:

          myfunction(inputdata,callbackFcn);

          The first argument is some object and the second is a callback function that fires when 'myfunction' is complete. The output data from the function is called asynchronously and is dumped to a callback function

          function callbackFcn(outputdata)
          {
             // do stuff with outputdata
          }

          I'd like to add a test point that verifies 'outputdata' for correctness. I can't call assertEquals(...) within callbackFcn() because the YUI test harness will not be in the call stack.

          function callbackFcn(outputdata)
          {
             assertEquals(true,...); // doesn't work, results in exception not YUI error
          }

          In your description of 2.4 below, it appears that I have pass in a wait time. After that time is up, the assertEquals(...) runs. Is that correct?

          The problem with that design is that I now have to hard code wait times into my tests. I'd rather not have to do that. I'd like for the assertEqual comparison to run immediately after the callback function fires. I don't want to add a time buffer since that could slow my tests down and lead to guess work on my part.

          Another way to think about it, there are two failure modes with any asynchronous API:
          a) callbackFcn never firing
          b) invalid outputdata

          I'm more interested in testing for b. Case a is not something I'm concerned with. If it does happen, my entire test would hang it would be clearly obvious something fundamental is wrong.

          Let me know if this doesn't clear things up,

          Joe





          On Nov 26, 2007 3:25 PM, Nicholas Zakas <nzakas@... > wrote:

          Hi Joe,

           

          Asynchronous testing is not possible in the 2.3 version of YUI Test. However, it will be available in the 2.4 version via the wait() method. It's a little bit difficult to tell what you're trying to accomplish from your code, but in 2.4 you'll be able to do something like this:

           

          testFoo : function(){

              this.wait(function(){

                  YAHOO.util.Assert.areEqual(…);

           

              }, 5000);   //wait 5 seconds

          }

           

          If you can hold on until 2.4, this job will become much easier for you. J

           

          -Nicholas

           


          From: ydn-javascript@yahoogroups.com [mailto:ydn-javascript@yahoogroups.com] On Behalf Of Joe C
          Sent: Wednesday, November 14, 2007 6:16 AM
          To: ydn-javascript@yahoogroups.com
          Subject: [ydn-javascript] YUI Test, unit test for an asynchronous API

           

          Hello,

          I would like advice on a good strategy for a unit test on an API that is asynchronous. I want to verify the results of an API takes in a callback function that will fire at a later time. I don't want to have to use polling either - I'd prefer something more elegant.

          Here is what I'm doing now:

          testFoo : function () {
                myfunction(...,mycallback); //  mycallback fires asynchronously 
          }

          mycallback: function(response) {
              var Assert = YAHOO.util.Assert ;
              Assert.areEqual(...);
          }


          This design doesn't work through because the Assert is called from mycallback and not a test* function.

          I get an error in firebug:
          "uncaught exception: ComparisonFailure: Values should be equal. Expected: false (boolean) Actual:true (boolean)"

          This error is expected because the YUI Test infrastructure is designed to catch these exceptions from the test* functions.

          Thanks!

          -Joe

           

        • Joe C
          Nicholas, Ok, yes, the resume() concept will fulfill my requirements. I look forward to version 2.4 Joe ... Nicholas, Ok, yes, the resume() concept will
          Message 4 of 4 , Nov 30, 2007
          • 0 Attachment
            Nicholas,

            Ok, yes, the resume() concept will fulfill my requirements.

             I look forward to version 2.4

            Joe


            On Nov 29, 2007 2:57 PM, Nicholas Zakas < nzakas@...> wrote:

            Hi Joe,

             

            There's actually two ways to use the new wait() method. The first is to use it like setTimeout(), as I described. The second is to pass in no arguments at all, which causes the entire test process to stop. You can then resume the process by calling resume(). In your case, this may be more reasonable. Still, from your description, this may be a bit tricky to test. I think something like the following would work:

             

            testMyFunction: function () {

             

                var that = this;

                myfunction(inputdata, function(outputData){

             

                   //restart the test

                   that.resume(function(){

                        YAHOO.util.Assert.areEqual(5, outputData.value);

                   });

                });

             

                //wait until resume() is called

                this.wait();

            }

             

            This should work to test your function assuming it behaves the way I think it does (using setTimeout() to call the callback function?). It may be a little trickier to determine if the callback function was ever called. If you're using setTimeout(), you are virtually guaranteed that the function will be called, so is that a major concern for you?

             

            -Nicholas

             

             


            From: Joe C [mailto:joe.jconti@...]
            Sent: Thursday, November 29, 2007 11:39 AM
            To: Nicholas Zakas
            Subject: Re: [ydn-javascript] YUI Test, unit test for an asynchronous API

             

            Hi Nicholas,

            Thanks for responding. I'm not sure adding wait(...) time to YUI Test framework will help me. Here is a little more information on what I'm trying to do.

            The function I want to test has an API that looks like this:

            myfunction(inputdata,callbackFcn);

            The first argument is some object and the second is a callback function that fires when 'myfunction' is complete. The output data from the function is called asynchronously and is dumped to a callback function

            function callbackFcn(outputdata)
            {
               // do stuff with outputdata
            }

            I'd like to add a test point that verifies 'outputdata' for correctness. I can't call assertEquals(...) within callbackFcn() because the YUI test harness will not be in the call stack.

            function callbackFcn(outputdata)
            {
               assertEquals(true,...); // doesn't work, results in exception not YUI error
            }

            In your description of 2.4 below, it appears that I have pass in a wait time. After that time is up, the assertEquals(...) runs. Is that correct?

            The problem with that design is that I now have to hard code wait times into my tests. I'd rather not have to do that. I'd like for the assertEqual comparison to run immediately after the callback function fires. I don't want to add a time buffer since that could slow my tests down and lead to guess work on my part.

            Another way to think about it, there are two failure modes with any asynchronous API:
            a) callbackFcn never firing
            b) invalid outputdata

            I'm more interested in testing for b. Case a is not something I'm concerned with. If it does happen, my entire test would hang it would be clearly obvious something fundamental is wrong.

            Let me know if this doesn't clear things up,

            Joe





            On Nov 26, 2007 3:25 PM, Nicholas Zakas <nzakas@... > wrote:

            Hi Joe,

             

            Asynchronous testing is not possible in the 2.3 version of YUI Test. However, it will be available in the 2.4 version via the wait() method. It's a little bit difficult to tell what you're trying to accomplish from your code, but in 2.4 you'll be able to do something like this:

             

            testFoo : function(){

                this.wait(function(){

                    YAHOO.util.Assert.areEqual(…);

             

                }, 5000);   //wait 5 seconds

            }

             

            If you can hold on until 2.4, this job will become much easier for you. J

             

            -Nicholas

             


            From: ydn-javascript@yahoogroups.com [mailto:ydn-javascript@yahoogroups.com] On Behalf Of Joe C
            Sent: Wednesday, November 14, 2007 6:16 AM
            To: ydn-javascript@yahoogroups.com
            Subject: [ydn-javascript] YUI Test, unit test for an asynchronous API

             

            Hello,

            I would like advice on a good strategy for a unit test on an API that is asynchronous. I want to verify the results of an API takes in a callback function that will fire at a later time. I don't want to have to use polling either - I'd prefer something more elegant.

            Here is what I'm doing now:

            testFoo : function () {
                  myfunction(...,mycallback); //  mycallback fires asynchronously 
            }

            mycallback: function(response) {
                var Assert = YAHOO.util.Assert ;
                Assert.areEqual(...);
            }


            This design doesn't work through because the Assert is called from mycallback and not a test* function.

            I get an error in firebug:
            "uncaught exception: ComparisonFailure: Values should be equal. Expected: false (boolean) Actual:true (boolean)"

            This error is expected because the YUI Test infrastructure is designed to catch these exceptions from the test* functions.

            Thanks!

            -Joe

             


          Your message has been successfully submitted and would be delivered to recipients shortly.