Hi Olivier,

that should indeed be the behavior, but the test also passes if I
remove the throw Error() line in my Account constructor. This is
probably because the fail() in the testcase is directly caught by the
catch block underneath. I used to do it this way in AS2, but it seems
like the try/catch behavior is different in AS3.

Does anyone have any solution for this, or another way of testing
throw statements?

regards,
Christophe

--- In flexcoders@yahoogroups.com, "Stembert Olivier (BIL)"
<[EMAIL PROTECTED]> wrote:
>
> Hi Christophe,
>  
> It seems logic the fail() statement is not executed since it follows the
> new Account() statement which throws the exception.
> I'm not sure I understand what you mean...
>  
> Regards,
>  
> Olivier
> 
> ________________________________
> 
> From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
> Behalf Of herrodius
> Sent: Wednesday, January 17, 2007 11:53 AM
> To: flexcoders@yahoogroups.com
> Subject: [flexcoders] [Flexunit] - How to test throwing errors?
> 
> 
> 
> Hi all,
> 
> sorry for this slightly OT message.
> 
> I was wondering what the best way is to test for a method throwing an
> error. I have an Account class that takes a number:int and a
> name:String as constructor params. If an Account is instantiated with
> a null name, I want to throw an Error (IllegalArgumentException).
> 
> In my test, I instantiate a new Account with a null name inside a
> try/catch and fail immediately after the instantiation. However this
> does not seem to work. The fail never executes and the test passes. 
> See example:
> 
> var errorMessage:String = "Account constructor should throw
> IllegalArgumentException when passing 'null' name.";
> try {
> var a:Account = new Account(100, null);
> fail(errorMessage);
> }
> catch (e:Error) {}
> 
> The only way I can get this to work, is to check if the errorMessage
> in the catch block is the same as the error message in my test and
> then call fail() again.
> 
> var errorMessage:String = "Account constructor should throw
> IllegalArgumentException when passing 'null' name.";
> try {
> var a:Account = new Account(100, null);
> fail(errorMessage);
> }
> catch(e:Error) {
> if (e.message == errorMessage) {
> fail(errorMessage);
> }
> }
> 
> Seems a bit weird. The fail() in the try block is catched by the catch
> block. I thought this worked in AS2? Am I missing out on something or
> doing something wrong here?
> 
> thx in advance.
> 
> regards,
> Christophe
> 
> 
> 
>  
> 
> ---------------------
> 
> An electronic message is not binding on its sender.
> 
> Any message referring to a binding engagement must be confirmed in
> writing and duly signed.
> 
> ---------------------
> 
>  
> 
> 
> ---------------------
> An electronic message is not binding on its sender.
> Any message referring to a binding engagement must be confirmed in
writing and duly signed.
> ---------------------
>


Reply via email to