[flexcoders] [Flexunit] - How to test throwing errors?

2007-01-17 Thread herrodius
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



RE: [flexcoders] [Flexunit] - How to test throwing errors?

2007-01-17 Thread Stembert Olivier (BIL)
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.
-