首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CakePHP:测试-> ValidationException

CakePHP:测试-> ValidationException
EN

Stack Overflow用户
提问于 2012-12-24 20:47:23
回答 1查看 748关注 0票数 0

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

我的测试方法:

代码语言:javascript
复制
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');
    }
}

我的测试类:

代码语言:javascript
复制
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);
}

我的例外是:

代码语言:javascript
复制
**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.
EN

回答 1

Stack Overflow用户

发布于 2012-12-25 07:02:17

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

如果您想要调试它,请尝试确定

代码语言:javascript
复制
$this->validationErrors

包含在save()之后(在/而不是抛出异常)。那么您就知道验证失败的原因了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14021640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档