-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);
}

そして、次のようなすべての Exceptionhandling メソッド:

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 に答える