我目前正在为以下模型编写单元测试:
<?php
namespace App\Model\Table;
use App\Model\Entity\User;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* Users Model
*
* @property \Cake\ORM\Association\HasMany $Comments
* @property \Cake\ORM\Association\BelongsToMany $Albums
*/
class UsersTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('users');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('Comments', [
'foreignKey' => 'user_id'
]);
$this->belongsToMany('Albums', [
'foreignKey' => 'user_id',
'targetForeignKey' => 'album_id',
'joinTable' => 'users_albums'
]);
}
/**
* @Author: Mark van der Laan
* @Date: 23-02-2016
* @Description: Validating rules for the user model. Some additional, more complex validation rules are added.
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
// id
$validator
->integer('id')
->allowEmpty('id', 'create');
// username
$validator
->requirePresence('username', 'create')
->notEmpty('username')
// Enabled, just in case that the username will be an email address
->email('username')
->add('username', [
'length' => [
'rule' => ['minLength', 7],
'message' => 'Username needs to be at least 7 characters long!',
]
]);
// password
$validator
->requirePresence('password', 'create')
->notEmpty('password')
->add('password', [
'length' => [
'rule' => ['minLength', 7],
'message' => 'Password needs to be at least 7 characters long!',
]
]);
// sign_in_count
$validator
->integer('sign_in_count')
->requirePresence('sign_in_count', 'create')
->notEmpty('sign_in_count');
// ip address
$validator
->allowEmpty('current_sign_in_ip')
->requirePresence('current_sign_in_ip', 'create')
// Currently checking for both IPv4 and IPv6 addresses
->ip('current_sign_in_ip', 'both');
// active
$validator
->boolean('active')
->requirePresence('active', 'create')
->allowEmpty('active');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(['username']));
return $rules;
}
}
对我来说,测试validationDefault方法非常重要。我尝试使用以下代码片段进行测试:
public function testValidationDefault()
{
$data = ['username' => 'adminadmin@mixtureweb.nl',
'password' => 'testtest123',
'sign_in_count' => 0,
'current_sign_in_ip' => '127.0.0.1',
'active' => 'true'
];
$this->assertTrue($this->Users->save($data));
// $this->assertTrue($data);
}
当我试图这样做时,会出现错误提示说不应该将数组传递给assertTrue方法。因此,我正在寻找示例,但我找不到任何东西。有人可以提供一些参考资料吗?我想要找到如何单元测试验证规则的方法(到目前为止,我在文档中找不到任何内容)。
更新:
public function testValidationDefault()
{
$data = ['username' => 'adminadmin@mixtureweb.nl',
'password' => 'testtest123',
'sign_in_count' => 0,
'current_sign_in_ip' => '127.0.0.1',
'active' => true
];
$user = $this->Users->newEntity($data);
$saved = $this->Users->save($user);
$this->assertTrue($saved);
// $this->assertTrue($data);
}
这将会显示“失败,断言App\Model\Entity\User对象&0000000011b3c53b0000000040aca14b是真的”。有没有人知道我做错了什么?