php - kahlan - 单元测试不起作用 - 获取 - 异常`代码(0),消息“未定义变量`请求`
问题描述
我是 kahlan 单元测试的新手,我已经在 Symfony 上安装了它。尝试运行时出现以下错误 - vendor/bin/kahlan
C:\web\simple-symfony-api-v2\vendor\kahlan\kahlan\src\Scope.php
错误:/category/list x 它显示类别列表在第 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);
});
});
});
解决方案
在测试、调试和阅读谷歌/文档后,我发现我丢失了 - 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();
});
推荐阅读
- python - 如何使用 DictWriter 方法制作正确的 csv 文件
- go - 不走常规,频道按通话顺序工作吗?
- java - 无法导入 Room 数据库以在 Android 应用程序中使用
- java - 将此代码从 C# 转换为 Java,还有什么可以在 Java 中简化的吗?
- excel - ComboBox 不接受字符,但接受数值
- java - 如何通过java springboot使用聚合项目操作从文档中排除嵌套字段
- javascript - 节点js服务器端的多字段输入请求
- excel - 我无法合并到 AA 列
- c# - 从第三方网站重定向回 wpf 应用程序
- python - MLP的Hessian矩阵wrt参数不对称(TF2.0)