Page 1 of 1
assertError not working
Posted: Sat Oct 21, 2006 6:06 pm
by Ollie Saunders
I'm probably doing something stupid but this:
Code: Select all
ini_set('display_errors', true);
error_reporting(E_ALL);
require_once 'simpletest/unit_tester.php';
require_once 'simpletest/reporter.php';
function foo()
{
trigger_error('error');
}
class TestAssertError extends UnitTestCase
{
public function testCatchTriggeredError()
{
trigger_error($msg = 'error');
$this->assertError($msg);
}
public function testCatchExternalTriggeredError()
{
foo();
$this->assertError();
}
}
$testCase = new TestAssertError();
$reporter = new HtmlReporter(ini_get('default_charset'));
$testCase->run($reporter);
...is producing this:
Code: Select all
TestAssertError
Fail: testCatchTriggeredError -> Expected error not found at [/osis/x/test/www/index.php line 17]
Fail: testCatchExternalTriggeredError -> Expected error not found at [/osis/x/test/www/index.php line 22]
1/1 test cases complete: 0 passes, 2 fails and 0 exceptions.
How can I make the assertions catch the errors?
Posted: Sat Oct 21, 2006 7:07 pm
by Maugrim_The_Reaper
It's your default error level I think.
Your trigger_error() call does not specify a level, so it defaults to E_USER_NOTICE. It might work if you set a E_USER_ERROR...
Edit | I take that back. I ran it myself and the tests all passed...
1/1 test cases complete: 2 passes, 0 fails and 0 exceptions.
I'm using the current CVS version, but I can't think of any difference that would make...
Posted: Sat Oct 21, 2006 8:48 pm
by Ambush Commander
Yeah. I don't see any reason why that shouldn't work. What's your PHP and SimpleTest version?
Posted: Sun Oct 22, 2006 5:20 am
by Ollie Saunders
Dammit, I was hoping I was being stupid. And specifing E_USER_ERROR made no difference at all.
PHP 5.1.2
simpletest_1.0.1alpha3
Posted: Sun Oct 22, 2006 5:21 am
by Chris Corbyn
I try to always use the CVS version of SimpleTest since they seem to recommend that on SitePoint. Just check it out to a directory and keep updating your copy regularly. It's under a fairly active development.
Posted: Sun Oct 22, 2006 6:13 am
by Ollie Saunders
d11wtq wrote:I try to always use the CVS version of SimpleTest since they seem to recommend that on SitePoint. Just check it out to a directory and keep updating your copy regularly. It's under a fairly active development.
worked, thanks.

Posted: Sun Oct 22, 2006 7:46 am
by Maugrim_The_Reaper
It was alpha3 -
You should take a peek at using expectError() and expectException(). The error assertions are still supported but marked as deprecated in the code...
Code: Select all
/**
* @deprecated
*/
function assertError($expected = false, $message = '%s') {
$context = &SimpleTest::getContext();
$queue = &$context->get('SimpleErrorQueue');
return $queue->assertError($this->_coerceExpectation($expected), $message);
}
/**
* Prepares for an error. If the error mismatches it
* passes through, otherwise it is swallowed. Any
* left over errors trigger failures.
* @param SimpleExpectation/string $expected The error to match.
* @param string $message Message on failure.
* @access public
*/
function expectError($expected = false, $message = '%s') {
$context = &SimpleTest::getContext();
$queue = &$context->get('SimpleErrorQueue');
$queue->expectError($this->_coerceExpectation($expected), $message);
}
assertError is being retained for BC, but there are rafts of changes coming up in the near future that make expectError() a safer choice...
Posted: Mon Oct 23, 2006 11:01 am
by Ollie Saunders
duly noted.
Posted: Mon Oct 23, 2006 11:55 am
by Ollie Saunders
uh well assertError was working fine and now i'm trying to use expectError. This is pretty obviously a bug but does anyone know what is going on?
Code: Select all
ini_set('display_errors', true);
error_reporting(E_ALL);
require_once 'cvs/simpletest/unit_tester.php';
require_once 'cvs/simpletest/reporter.php';
class TestAssertError extends UnitTestCase
{
public function test1()
{
trigger_error('error'); // __LINE__ == 12
$this->expectError(true);
}
public function test2()
{
trigger_error('error');
$this->expectError(true);
}
}
$testCase = new TestAssertError();
$reporter = new HtmlReporter(ini_get('default_charset'));
$testCase->run($reporter);
produces
Code: Select all
TestAssertError
Exception: test1 -> Unexpected PHP error [error] severity [E_USER_NOTICE] in [/osis/x/test/www/index.php line 12]
1/1 test cases complete: 1 passes, 0 fails and 1 exceptions.
Which is basically saying that despite being identical test2 is fine but test1 isn't catching the error.
Posted: Mon Oct 23, 2006 12:55 pm
by Chris Corbyn
Usually if something is expect rather than assert you need to call it before hand. Try calling expectError() then generate the error.
Posted: Mon Oct 23, 2006 1:34 pm
by Ollie Saunders
that would explain A LOT! Thanks d11wtq