Copy link to clipboard
Copied
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.
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Todd,
I don't see anywhere where the textfield is added to the movieclip. Does that happen elsewhere?
Thanks,
Mike
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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....
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
>>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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
No problem. Let me know if I can help in any other way.