使用cakephp3和PHPUnit测试add()方法';行不通
我使用了bake来生成用户的控制器表,现在我需要使用使用cakephp3和PHPUnit测试add()方法';行不通,cakephp,phpunit,cakephp-3.0,Cakephp,Phpunit,Cakephp 3.0,我使用了bake来生成用户的控制器表,现在我需要使用PHPUnit和cakephp3来测试数据库中是否输入了一条记录,但我看不出这是否真的被插入了 我无法应用此解决方案: 第一个错误: 1) App\Test\TestCase\Controller\UsersControllerTest::testAdd失败 断言“”包含“用户已保存” 如果删除断言(或更改为assertResponseSuccess) 在assertEquals中,数组$result为空 在UsersController中添加
PHPUnit
和cakephp3
来测试数据库中是否输入了一条记录,但我看不出这是否真的被插入了
我无法应用此解决方案:
第一个错误:
1) App\Test\TestCase\Controller\UsersControllerTest::testAdd失败
断言“”包含“用户已保存”
如果删除断言(或更改为assertResponseSuccess)
在assertEquals中,数组$result
为空
在UsersController中添加操作:
public function add()
{
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$userTypes = $this->Users->UserTypes->find('list', ['limit' => 200]);
$this->set(compact('user', 'userTypes'));
$this->set('_serialize', ['user']);
}
UsersControllerTest中的testAdd:
public function testAdd()
{
$this->get('/users/add');
$this->assertResponseOk();
//-------------------------------------------------------------------------
$data = [
'id' => 999999,
'email' => 'usuariocomum999999@gmail.com',
'password' => 'usuariocomum999999senha',
'username' => 'usuariocomum999999username',
'user_type_id' => 900000,
'created' => '2014-07-17 18:46:47',
'modified' => '2015-07-17 18:46:47'
];
$this->post('/users/add', $data);
$this->assertResponseContains('The user has been saved.');
//-------------------------------------------------------------------------
$expected = [
[
'id' => 999999,
'email' => 'usuariocomum999999@gmail.com',
'password' => 'usuariocomum999999senha',
'username' => 'usuariocomum999999username',
'user_type_id' => 900000,
'created' => new Time('2014-07-17 18:46:47'),
'modified' => new Time('2015-07-17 18:46:47')
]
];
$users = TableRegistry::get('Users');
$query = $users->find('all', [
'fields' => ['Users.id', 'Users.email', 'Users.password',
'Users.username', 'Users.user_type_id', 'Users.created',
'Users.modified'],
'conditions' => ['Users.id' => 999999]
]);
$result = $query->hydrate(false)->toArray();
$this->assertEquals($expected, $result);
}
数据源测试:
'test' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
//'port' => 'nonstandard_port_number',
'username' => 'shop',
'password' => 'shop',
'database' => 'shoppingtest',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
]
注意:CakePHP 3.0.11和PHPUnit 4.8.6您需要测试
会话中是否有重定向和成功消息,响应将不包含您的文本,因为响应只是浏览器的重定向头代码。您可以举个例子吗?
'test' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
//'port' => 'nonstandard_port_number',
'username' => 'shop',
'password' => 'shop',
'database' => 'shoppingtest',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
]