使用CakePHP 3测试delete()方法并且PHPUnit不起作用

时间:2015-09-02 19:11:38

标签: cakephp phpunit cakephp-3.0

我使用 bake 生成用户表的控制器,现在我需要使用PHPUnit和CakePHP 3进行测试已被删除。

没有提示错误,只是测试失败。

  

我无法应用此解决方案:Solution

在UsersControllerTest中的testDelete:

public function testDelete()
{
    $this->get('/users/delete/900000');

    $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' => 900000]
    ]);
    $result = $query->hydrate(false)->toArray();
    $this->assertEmpty($result);
}

UsersController中的删除方法:

public function delete($id = null)
{
    $user = $this->Users->get($id);
    if ($this->Users->delete($user)) {
        $this->Flash->success(__('The user has been deleted.'));
    } else {
        $this->Flash->error(__('The user could not be deleted. Please, try again.'));
    }
    return $this->redirect(['action' => 'index']);
}

数据源测试:

'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

1 个答案:

答案 0 :(得分:0)

试试:

$query = $users->find('all', [
            'fields' => ['Users.id', 'Users.email', 'Users.password',
                'Users.username', 'Users.user_type_id', 'Users.created',
                'Users.modified'],
            'conditions' => ['Users.id' => 900000]
        ]);
        $result = $query->hydrate(false)->toArray();
        $result_count = count($result);
        $expected_count = 0;
        $this->assertEquals($expected_count, $result_count);