kahlan-单元测试不起作用-得到-带有消息“未定义变量`request`的代码”(0)

时间:2018-10-14 22:49:58

标签: php unit-testing symfony4

我是kahlan单元测试的新手,我已将其安装在Symfony上。尝试运行时出现以下错误-vendor / bin / kahlan

错误: /类别/列表     x它显示类别列表       C:\web\simple-symfony-api-v2\vendor\kahlan\kahlan\src\Scope.php第109行中抛出了未捕获的异常

  message:`Exception` Code(0) with message "Undefined variable `request`."

代码:

namespace App\Spec\Controller;

describe('CategotyController', function() {
  describe('/category/list', function() {
    it('shows category list', function() {

      $request = $this - > request - > create('/category/list', 'GET');
      $response = $this - > kernel - > handle($request);

      var_dump($response);
    });
  });
});

1 个答案:

答案 0 :(得分:0)

经过测试,调试和对google / documentation的一些阅读后,我发现我不见了-kahlan-config.php-该文件需要添加到Symfony项目的根目录中。

代码:

<?php // kahlan-config.php 

use App\Kernel;
use Kahlan\Filter\Filters;
use Symfony\Component\HttpFoundation\Request;

Filters::apply($this, 'bootstrap', function($next) {

    require __DIR__ . '/vendor/autoload.php';

    $root = $this->suite()->root();
    $root->beforeAll(function () {
       $this->request = Request::createFromGlobals();
       $this->kernel  = new Kernel('test', false);
    });

    return $next();

});