【问题标题】:CakePHP: testing -> ValidationExceptionCakePHP:测试-> ValidationException
【发布时间】:2012-12-10 21:26:12
【问题描述】:

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

我的测试方法:

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.

【问题讨论】:

  • 显然您没有满足Institution 模型中设置的验证规则。
  • 另外,如果你没有在任何地方捕获异常,在这里抛出异常可能是不明智的。如果所有验证错误都触发 404 等,用户很快就会对你非常生气,他们将不得不从头开始填写你的表单。
  • @mark: 没问题,在别处被抓到了!

标签: cakephp testing phpunit


【解决方案1】:

正如 cmets 中所述,这个问题非常直接。您的验证失败,因此抛出异常(根据您的代码的需要)。

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

$this->validationErrors

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 2015-11-17
    • 2013-12-29
    • 2018-03-10
    • 2016-04-10
    • 1970-01-01
    相关资源
    最近更新 更多