0

我是编程和测试的新手。我被困在这个测试中,它抛出了一个异常。请帮助我,因为我真的不明白我应该如何处理这个问题。

我的测试方法:

public function saveNewInstitution($institutionData, $user) {
    $institutionData[$this->alias]['isActive'] = 1;
    $institutionData[$this->alias]['users_id'] = $user['User']['id'];
    $this->create();
    $this->set($institutionData);
    if ($this->validates()) {
        return $this->save();
    } else {
        throw new ValidationException('Not all fields are filled out correct');
    }
}

我的测试课:

public function testSaveNewInstitution() {
    $result = array(
        'Institution' => array(
            'id' => 2,
            'name' => 'Spitex',
            'address' => 'Hauptweg 4',
            'postcode' => '1234',
            'city' => 'huuh',
            'phone_number' => '123 456 78 90',
            'email' => 'staufen@tsdy.huuh',
            'comment' => '',
            'isActive' => TRUE,
            'users_id' => 2,
            'institution_types_id' => 5
        ),
        'Users' => array(
            'id' => 2,
            'email' => 'herbert@xyz.ch',
            'password' => AuthComponent::password('12345678'),
            'isMale' => TRUE,
            'first_name' => 'Herbert',
            'last_name' => 'Müller',
            'address' => 'Hauptstrasse 1',
            'postcode' => '1234',
            'city' => 'Zürich',
            'phone_number' => '123 456 78 90',
            'isActive' => FALSE,
            'institutions_id' => 2,
            'groups_id' => 4
        ),
        'InstitutionTypes' => array(
            'id' => 5,
            'name' => 'Spitex'
        ),
        'Assignee' => array('0' => array(
                'id' => 2,
                'email' => 'herbert@xyz.ch',
                'password' => AuthComponent::password('12345678'),
                'isMale' => TRUE,
                'first_name' => 'Herbert',
                'last_name' => 'Müller',
                'address' => 'Hauptstrasse 1',
                'postcode' => '1234',
                'city' => 'Zürich',
                'phone_number' => '123 456 78 90',
                'isActive' => FALSE,
                'institutions_id' => 2,
                'groups_id' => 4
        ))
    );

    $expected = $this->Institution->saveNewInstitution($result, 2);
    $this->assertEqual($result, $expected);
}

public function testSaveNewInstitutionException() {
    $this->setExpectedException('ValidationException');
    $expected = array(
        'Institution' => array(
            'id' => 2,
            'name' => 'Spitex',
            'address' => 'Hauptweg 4',
            'postcode' => '1234',
            'city' => 'huuh',
            'phone_number' => '123 456 78 90',
            'email' => 'staufen@xyz.huuh',
            'comment' => '',
            'isActive' => TRUE,
            'users_id' => 2,
            'institution_types_id' => 5
        ),
        'Users' => array(
            'id' => 2,
            'email' => 'herbert@xyz.ch',
            'password' => AuthComponent::password('12345678'),
            'isMale' => TRUE,
            'first_name' => 'Herbert',
            'last_name' => 'Müller',
            'address' => 'Hauptstrasse 1',
            'postcode' => '1234',
            'city' => 'Zürich',
            'phone_number' => '123 456 78 90',
            'isActive' => FALSE,
            'institutions_id' => 2,
            'groups_id' => 4
        ),
        'InstitutionTypes' => array(
            'id' => 5,
            'name' => 'Spitex'
        ),
        'Assignee' => array('0' => array(
                'id' => 2,
                'email' => 'herbert@mueller.ch',
                'password' => AuthComponent::password('12345678'),
                'isMale' => TRUE,
                'first_name' => 'Herbert',
                'last_name' => 'Müller',
                'address' => 'Hauptstrasse 1',
                'postcode' => '1234',
                'city' => 'Zürich',
                'phone_number' => '123 456 78 90',
                'isActive' => FALSE,
                'institutions_id' => 2,
                'groups_id' => 4
        ))
    );
    $this->Institution->saveNewInstitution($expected, 2);
}

我的例外:

**VALIDATIONEXCEPTION**
Not all fields are filled out correct
Test case: InstitutionTest(testSaveNewInstitution)
Stack trace:
/app/Test/Case/Model/InstitutionTest.php : 148
InstitutionTest::testSaveNewInstitution
/usr/lib/php/PHPUnit/Framework/TestCase.php : 969
/usr/lib/php/PHPUnit/Framework/TestCase.php : 824
/usr/lib/php/PHPUnit/Framework/TestResult.php : 648
/usr/lib/php/PHPUnit/Framework/TestCase.php : 769
/lib/Cake/TestSuite/CakeTestCase.php : 78
/usr/lib/php/PHPUnit/Framework/TestSuite.php : 775
/usr/lib/php/PHPUnit/Framework/TestSuite.php : 745
/usr/lib/php/PHPUnit/TextUI/TestRunner.php : 346
/lib/Cake/TestSuite/CakeTestRunner.php : 57
/lib/Cake/TestSuite/CakeTestSuiteCommand.php : 111
/lib/Cake/TestSuite/CakeTestSuiteDispatcher.php : 242
/lib/Cake/TestSuite/CakeTestSuiteDispatcher.php : 99
/lib/Cake/TestSuite/CakeTestSuiteDispatcher.php : 116
/app/webroot/test.php : 92
9/9 test methods complete: 8 passes, 0 fails, 12 assertions and 1 exceptions.
4

1 回答 1

0

正如评论中所述,这个问题非常简单。您的验证失败,因此抛出异常(如您的代码所期望的那样)。

如果你想调试它,试着确定什么

$this->validationErrors

包含在 save() 之后(在/而不是抛出异常之前)。那么你知道为什么验证失败了。

于 2012-12-24T23:02:16.547 回答