|
From: Jeff M. <je...@pr...> - 2004-02-15 04:21:33
|
On Feb 14, 2004, at 11:03 PM, Jason Sweat wrote:
> --- Jeff Moore <je...@pr...> wrote:
>> Hello,
>>
>> I was looking for some advise regarding testing for errors. If I have
>> some code that I want to test of the form:
>>
>> if (condition1) {
>> trigger_error()
>> }
>>
>> if (condition2) {
>> trigger_error()
>> }
>>
>> And I have the following test case
>>
>> // trigger error condition 1
>> $this->assertError();
>
> Marcus has conditioned me to use the assertErrorPattern() method.
>
> http://simpletest.sourceforge.net/api/SimpleTest/UnitTester/
> UnitTestCase.html#assertErrorPattern
About 99.5% of the errors that I want to test for were triggered by the
WACT RaiseError() function. Since the RaiseError signature involves
only error codes and data, it should be possible to test for the items
directly. No regular expressions required.
For example, the call:
RaiseError('compiler', 'VALUENOTFOUND', array(
'file' => $file,
'section' => $section,
'key' => $key));
could be directly tested for with a statement similar to
$this->expectError(BuildWactError('compiler', 'VALUENOTFOUND', array(
'file' => '/filename.ini', 'section' => 'compiler', 'key' =>
'path')));
> I would say your best bet would be to set up the test to meet each or
> none, one
> or both of the conditions (perhaps four separate methods in the test
> case?),
> and then explicitly test for the expected errors.
The problem is that condition2 is dependant upon condition1. If
condition1 is true, condition2 will always be true as well. There is
no way to re-arrange the test cases to avoid getting two errors. It
would be possible to re-arrange the code, but that doesn't seem like
the right thing.
(if you are interested, the WACT test case that triggered this
discussion is
cases/template/tags/core/include.test.php->CoreIncludeTagTestCase-
>testmissingfileattribute
>
>> Should I put a swallowErrors() call after my assertError() to prevent
>> the exceptions?
>>
>> If so, should I put a swallowErrors() call at the end of every test
>> case that calls assertError?
>
> I would avoid use of swallowErrors at all, I have found this got rid
> of error
> that crept in unexpectedly on me. YMMV
Hmmm. :(
|