-1

我的验证失败,所以我对此消息的测试ValidationException每次都会抛出一个。“并非所有字段都填写正确”。为什么?类似地,它适用于用户(另一种方法)。

模型:

public $validate = array(
    'name' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'Name can not be empty',
        ),
        'unique' => array(
            'rule' => 'isUnique',
            'message' => 'This name is already used.',
        ),
    ),
    'address' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'Address can not be empty',
        ),

...

public function saveNewInstitution($institutionData, $userId) {
    $institutionData[$this->alias]['is_active'] = 1;
    $institutionData[$this->alias]['user_id'] = $userId;
    $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() {
    $data = array(
        'Institution' => array(
            'name' => 'Spitex Staufen',
            'address' => 'Hauptweg 4',
            'postcode' => '1234',
            'city' => 'huuh',
            'phone_number' => '123 456 78 90',
            'email' => 'staufen@spitex.huuh',
            'comment' => '',
            'institution_type_id' => 5
            ));
    $expected = array(
        'Institution' => array(
            'id' => 2,
            'name' => 'Spitex Staufen',
            'address' => 'Hauptweg 4',
            'postcode' => '1234',
            'city' => 'huuh',
            'phone_number' => '123 456 78 90',
            'email' => 'staufen@spitex.huuh',
            'comment' => '',
            'is_active' => TRUE,
            'user_id' => 2,
            'institution_type_id' => 5
            ));

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

以及所有像这样的异常处理方法:

public function testSaveNewInstitutionExceptionName() {
    $this->setExpectedException('ValidationException');
    $this->Institution->saveNewInstitution($this->__nameEmpty, 2);
}
4

1 回答 1

0

我不知道我是否真的明白了 - 你的 Testclass 是失败了还是 saveNewInstitution() 函数给你带来了麻烦?您是否尝试手动检查 saveNewInstitution() 是否正常工作 - 将一些数据以表格形式传递给您的函数?

也许这只是一件小事,并且您的数据库中有一个 id=2 的条目,并且您有一个重复的键异常被您自己的异常方法 testSaveNewInstitutionExceptionName() 覆盖。

于 2012-12-28T14:54:12.743 回答