16 Replies Latest reply on Jul 11, 2010 2:04 PM by mlabriola

    flexunit 4 fails for me

    toddcoulson Level 1

      I am simply running an Assert.assertTrue call and it says it fails, here is my code:

      package sampleSuite.tests
      {
           import org.flexunit.*;
           public class TestCase1
           {
                [Before]
                 public function setUp():void
                 {
                 }
       
                 [After]
                 public function tearDown():void
                 {
                 }
       
                 [BeforeClass]
                 public static function setUpBeforeClass():void
                 {
                 }
       
                 [AfterClass]
                 public static function tearDownAfterClass():void
                 {     
                 }
      
                [Test]
                public function testFunction():void{
                     Assert.assertTrue();
      
                }
           }
      }
      

      Pretty straightforward code, but it keeps giving me an error expected true but returns false. TIA.

        • 1. Re: flexunit 4 fails for me
          toddcoulson Level 1

          I have another problem too. I placed the hamcrest.swc in my library and tried to make calls to hamcrest calls and they return errors. I downloaded the latest flexunit4, but still running into problems. Here is my code:

          [Test( description = "This checks movieclips for textfields" )]
                    public function checkTexts():void 
                    {
                         var arr:Array = ChildObjects.findChildTextFields(mc);
                             assertThat( arr, hasItems(tf) );
                         
                    }
          

          I was able to get an AssertEquals to register as correct, but have failed in everything else I have tried. I am using Flex Builder 3.

           

          Todd

          • 2. Re: flexunit 4 fails for me
            mlabriola Level 4

            Todd:

             

            assertTrue() takes an argument. You are not passing one, so you are saying assertTure( null ), which FlexUnit is correctly interpreting as not true and failing an assertion.

             

            try assertTrue( true ) or assertTrue( 1 == 1 ) and you will see that it works just fine.

             

            Mike

            • 3. Re: flexunit 4 fails for me
              mlabriola Level 4

              Well, I don't know what the rest of your code looks like, but if I do a simple test of hamcrest using an array and finding things, it works well. I would suggest perhaps verifying that your installation works with something simple like an array of numbers, finding a number, etc.

               

              I assure you the code works well, so if you want to post the whole test, including where mx and tf are defined, I can look further.

               

              Mike

              • 4. Re: flexunit 4 fails for me
                mlabriola Level 4

                Todd,

                 

                Can you post your code for that last example if you get time? It might be nice to have an example here in the forums that people can look back to.

                 

                Thanks,

                Mike

                • 5. Re: flexunit 4 fails for me
                  toddcoulson Level 1

                  thanks for your answers.  I am sure a lot of this is setup problems that I am having. I am just getting into FlexUnit and just struggling through the early setup.

                   

                  The mc and tf variables are not the problem, they are not throwing any errors, it is the call to assertThat and hasItems that are both showing problems:


                  1180: Call to a possibly undefined method assertThat. 

                  and

                  1180: Call to a possibly undefined method hasItems. 

                   

                  Todd

                  • 6. Re: flexunit 4 fails for me
                    toddcoulson Level 1
                    package sampleSuite.tests
                    {
                         import com.bluediesel.*;
                         import com.bluediesel.utils.ChildObjects;
                         
                         import flash.display.MovieClip;
                         import flash.text.TextField;
                         
                         import org.flexunit.*;
                         import org.hamcrest.*;
                         
                         public class TestCase1
                         {
                              private var mc:MovieClip;
                              private var tf:TextField;
                              
                              [Before]
                               public function setUp():void
                               {
                                    mc = new MovieClip();
                                    tf = new TextField();
                                    tf.name = "myTextName";
                               }
                     
                               [BeforeClass]
                               public static function setUpBeforeClass():void
                               {
                               }
                               
                    
                              [Test( description = "This tests addition" )]
                              public function simpleAdd():void 
                              {
                                   var x:int = 5 + 3;
                                   Assert.assertEquals( 8, x );
                              }
                              
                              [Test( description = "This checks movieclips for textfields" )]
                              public function checkTexts():void 
                              {
                                   var arr:Array = ChildObjects.findChildTextFields(mc);
                                  assertThat( arr, hasItems(tf) );
                                   
                              }
                              
                              [After]
                               public function tearDown():void
                               {
                               }
                               [AfterClass]
                               public static function tearDownAfterClass():void
                               {     
                               }
                         }
                    }
                    

                    Here is my code anyway.

                    • 7. Re: flexunit 4 fails for me
                      mlabriola Level 4

                      Todd,

                       

                      I don't see anywhere where the textfield is added to the movieclip. Does that happen elsewhere?

                       

                      Thanks,

                      Mike

                      • 8. Re: flexunit 4 fails for me
                        mlabriola Level 4

                        Sorry,

                         

                        Also just reread your last message. Regarding the assertThat, I am guessing FlexBuilder is just messing with you, it seems to like to do that.

                         

                        Ensure you have these two imports:

                         

                         

                         

                        import org.flexunit.assertThat;

                         

                        import org.hamcrest.collection.hasItems;

                         

                         

                        Thanks,

                        Mike

                        1 person found this helpful
                        • 9. Re: flexunit 4 fails for me
                          toddcoulson Level 1

                          My code has

                          import org.flexunit.*;
                              import org.hamcrest.*;

                          In it. Which should import both packages. That should cover the things you said. I have the hamcrest and flexunit4 swc files inside my library build path. What is weird is that Flex will allow me to see the code hints for these method calls, but when I use them, they don't work and have build errors.

                          • 10. Re: flexunit 4 fails for me
                            mlabriola Level 4

                            Could you try to add the two import statements I called out and not the others to see if it is flex builder messing with you as I suggested....

                            • 11. Re: flexunit 4 fails for me
                              mlabriola Level 4

                              there is an assertThat in the latest hamcrest package in the dir you are importing in addition to the flexunit one, this is causing flexbuilder to be unable to resolve.

                               

                              org.flexunit.assertThat() will clean up your compilation errors. We will need to coordinate with the hamcrest folks on how to handle this.

                               

                              Mike

                               

                               

                              1 person found this helpful
                              • 12. Re: flexunit 4 fails for me
                                toddcoulson Level 1

                                yeah, good answer, flex builder was messing with me. Nice, the imports you put in worked.

                                Actually, I spoke too soon, Flex must have still been cleaning, the error is still there.

                                Spoke too soon again, the hamcrest calls are working for me now again. Pretty flaky in flex 3 though. Hope this is better in flash builder.

                                 

                                Thanks again.

                                • 13. Re: flexunit 4 fails for me
                                  psaieg

                                  I am not sure if this is the right place to post this, but I have found two problems with assertThat() in FB4:

                                   

                                  1. FB4 will not register a test containing (only) an org.hamcrest.assertThat() as a test, and therefore will not run in, (or as in my case, since I was only using hamcrests assertThat, it will tell you that there are not tests to run in your file)

                                   

                                  2. org.flexunit.assertThat() explodes on chained matchers. The following causes a stack trace with org.flexunit.assertThat(), but works fine with org.hamcrest.assertThat():

                                   

                                  [Test]
                                          public function should_chain_matchers_together():void
                                          {
                                              assertThat("good", both(equalTo("good")).and(not(equalTo("bad"))));
                                              assertThat("good", either(equalTo("good")).or(not(equalTo("bad"))));

                                          }

                                   

                                  3. is there anyway to get an updated flexunitextended.swc such that it 'is in sync' with the most recent flexunit4 (or 4.1 beta as the case may be)?


                                  Anyways, I am blocked on moving my team to unit testing in FB4 at the moment because of these issues. Thanks for anything you can find out -- please let me know if there is anything I can do to work around/fix these issues.

                                  • 14. Re: flexunit 4 fails for me
                                    mlabriola Level 4

                                    >>1. FB4 will not register a test containing (only) an org.hamcrest.assertThat() as a test, and therefore will not run in, (or as in my case, since I was only >>using hamcrests assertThat, it will tell you that there are not tests to run in your file)

                                     

                                    I am not sure what you mean by this. Anything marked with [Test], even a completely blank method, is a valid test:

                                     

                                    [Test]

                                    public function doNothing():void

                                    {

                                    }

                                     

                                    That is valid.  Do you mean you are putting an assertThat() in a method but *not* marking it with [Test]? The definition of a test is based on metadata, not contents of the method.

                                     

                                    >>2. org.flexunit.assertThat() explodes on chained matchers. The following causes a stack trace with org.flexunit.assertThat(), but works fine >>with org.hamcrest.assertThat():

                                     

                                    I take your code exactly as listed in your post and it ran correctly for me. I have included the entire test case for your reference:

                                     

                                    package

                                    {

                                    import org.flexunit.assertThat;

                                    import org.hamcrest.core.both;

                                    import org.hamcrest.core.either;

                                    import org.hamcrest.core.not;

                                    import org.hamcrest.object.equalTo;

                                     

                                    public class MyTestCase

                                    {

                                    [Test]

                                    public function should_chain_matchers_together():void

                                    {

                                    assertThat("good", both(equalTo("good")).and(not(equalTo("bad"))));

                                    assertThat("good", either(equalTo("good")).or(not(equalTo("bad"))));

                                    }

                                    }

                                    }

                                     

                                    If this fails for you, then let me know your version numbers for both the hamcrest and flexunit swc and I will make sure that I test against the same version.

                                     

                                    >>3. is there anyway to get an updated flexunitextended.swc such that it 'is in sync' with the most recent flexunit4 (or 4.1 beta as the case may be)?

                                     

                                    flexunitextended.swc just holds Adobe's components for interfacing with FlexUnit. It doesn't hold any flexunit resources itself so it doesn't change regardless if you use 4.0 or the betas, etc. If you look in the same directory include that links in the extended classes, you will see something like:

                                     

                                    flexunit-core-flex-4.0.0.2-sdk3.5.0.12683.swc

                                     

                                    This is the swc that you actually need to change. Flash Builder will also show you the path on your drive where that file exists. You can replace that file by literally renaming it to a .old file on the file system and copying a newer swc into that directory. That is the easiest way. If you feel comfortable, you can instead edit those link properties to remove adobe's entry and instead link your own swc and then relink adobe's directly as opposed to a directory. This is more complicated than it should be but I am pushing Adobe to fix this in future versions.

                                     

                                    Mike

                                    • 15. Re: flexunit 4 fails for me
                                      psaieg Level 1

                                      Thanks for the quick reply, it seems my real problem must have been a corrupt workspace file somewhere. Just to be sure everything was cool, I rebuilt the workspace / project from scratch. Low and behod, no more errors. Thanks again.

                                       

                                      - Paul

                                      • 16. Re: flexunit 4 fails for me
                                        mlabriola Level 4

                                        No problem. Let me know if I can help in any other way.