Page 1 of 1
assertError
Posted: Tue Nov 08, 2005 3:52 am
by Ree
I am trying to get into unit testing lately and got a simple question. When exactly is assertError and its variants used? I am using SimpleTest, btw.
Posted: Tue Nov 08, 2005 10:05 am
by Ambush Commander
There is information in the
documentation.
Marcus Baker wrote:Code: Select all
trigger_error('Disaster');
trigger_error('Catastrophe');
$this->assertError();
$this->assertError('Catastrophe');
$this->assertNoErrors();
This one takes some explanation as in fact they all pass!
PHP errors in SimpleTest are trapped and placed in a queue. Here the first error check catches the "Disaster" message without checking the text and passes. This removes the error from the queue. The next error check tests not only the existence of the error, but also the text which here matches so another pass. With the queue now empty the last test will pass as well. If any unchecked errors are left at the end of a test method then an exception will be reported in the test. Note that SimpleTest cannot catch compile time PHP errors.
Posted: Tue Nov 08, 2005 11:07 am
by Ree
Right, I have missed it for some reason. However, I would like my classes to be aware of any errors themselves, without using PHP errors. I created a simple database connect-disconnect test case:
Code: Select all
function testDisconnectAfterUnsuccessfullConnection()
{
$db = &new Database('foobar', db_name, db_user, db_pass);
$db->disconnect();
$this->assertIdentical($db->getError(), 'NoConnection');
}
It passes all right, BUT it throws an exception (SimpleTest caught PHP native error) which, as you could've guessed, occurs in mysql_connect() call (wrong host). Well, I do know, that IF I set error reporting to 0 in my script, I will not get the error and I know my class will work just fine in this case since it passes the test. I can put $this->assertError() in the test and the exception will be gone... but I just thought myself, it looks like I have to 'fix' my tests to get green?? Shouldn't I be fixing my classes instead?? So, is the assertError used in tests only to get around the native PHP errors, or is there another purpose I do not know?
Posted: Tue Nov 08, 2005 11:23 am
by Ambush Commander
You suppress the error using @.
Posted: Tue Nov 08, 2005 11:58 am
by Ree
Yes, then what is the purpose of assertError?
Posted: Tue Nov 08, 2005 12:03 pm
by Ambush Commander
SimpleTest's assertError is for native PHP errors. Since you've decided to roll your own error system, it's a bit of a null point, as you thought.
However, SimpleTest makes it easy for you to overload the assertError() assertion and supplant it with something that plays nice with your own error checking (even if it's as simple as a wrapper for AssertIdentical).
I wouldn't be so quick to dismiss the value of traditional errors: by defining your own handlers, you can do some pretty powerful stuff. However, at the end of the day, what we really want to use is Exceptions.
I can't wait til the day SimpleTest works in PHP5.
Posted: Tue Nov 08, 2005 3:40 pm
by McGruff
I'm afraid I've been too busy working with what I've got in php4 to look at php5 properly. I haven't got a clue what SimpleTest does with exceptions.
I'll really need to get up to speed - especially now that Gentoo is making it easy to install v4 and v5 together.
Posted: Tue Nov 08, 2005 3:51 pm
by Ambush Commander
SimpleTest calls them exceptions, but they're not really exceptions. PHP's set_error_handler gives you, at best, a crude substitute for exceptions. SimpleTest capitalizes on this fact: the same way exceptions jump to the catch block, SimpleTest checks if any "try/catch blocks" (really assertError()) have been set, otherwise, it throws a warning: the equivalent of "uncaught exception" except that it's not fatal.