assertError
Moderator: General Moderators
assertError
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.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
There is information in the documentation.
Marcus Baker wrote:This one takes some explanation as in fact they all pass!Code: Select all
trigger_error('Disaster'); trigger_error('Catastrophe'); $this->assertError(); $this->assertError('Catastrophe'); $this->assertNoErrors();
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.
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:
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?
Code: Select all
function testDisconnectAfterUnsuccessfullConnection()
{
$db = &new Database('foobar', db_name, db_user, db_pass);
$db->disconnect();
$this->assertIdentical($db->getError(), 'NoConnection');
}- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
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.
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.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
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.