Cakephp 3单元测试无法发布数据

时间:2018-03-29 11:52:44

标签: unit-testing cakephp cakephp-3.x

我使用邮递员运行api,它有效。但是我为它创建的单元测试不起作用

以下是我的单元测试文件

<?php
namespace App\Test\TestCase\Controller\Api;

use App\Controller\Api\EmployeesController;
use Cake\TestSuite\IntegrationTestCase;

/**
 * App\Controller\Api\EmployeesController Test Case
 */
class EmployeesApiControllerTest extends IntegrationTestCase
{

    /**
     * Fixtures
     *
     * @var array
     */
    public $fixtures = [
        'app.projects',
        'app.projects_sites',
        'app.transactions',
        'app.sites'
    ];

    /**
    * Test REST API PIN login
    */

    public function testJwtToken() {
      $this->configRequest([
          'headers' => [
              'Accept' => 'application/json',
              'Content-Type' => 'application/json'
          ]
      ]);
      $this->post('/api/employees/token', ['pin_code' => '1001']);
      $this->assertResponseOk();
    }
}

这是运行后的控制台响应

$ docker-compose run webapp vendor/bin/phpunit tests/TestCase/Controller/Api/EmployeesApiControllerTest.php 
Starting timesheetcakephp_mysql_1 ... done
PHPUnit 6.5.7 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 1.52 seconds, Memory: 6.00MB

There was 1 failure:

1) App\Test\TestCase\Controller\Api\EmployeesApiControllerTest::testJwtToken
Status code is not between 200 and 204
Failed asserting that 500 is equal to 204 or is less than 204.

/var/www/html/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestCase.php:796
/var/www/html/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestCase.php:713
/var/www/html/tests/TestCase/Controller/Api/EmployeesApiControllerTest.php:37

FAILURES!
Tests: 1, Assertions: 4, Failures: 1.

调试日志不断显示此

2018-03-29 19:47:13 Debug: duration=0 rows=0 SELECT Employees.id AS `Employees__id`, Employees.project_id AS `Employees__project_id`, Employees.name AS `Employees__name` FROM employees Employees WHERE Employees.pin_code = NULL LIMIT 1

我不明白为什么当我在单元测试中明确说明'1001'时,pin_code一直声明为NULL。

0 个答案:

没有答案