【发布时间】:2021-08-31 07:00:27
【问题描述】:
如何防止phpunit启动我不想要的功能?
<?php
namespace App\Tests;
use App\Core\Security\ModuleService;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class moduleTest extends WebTestCase
{
/**
* @var ModuleService
*/
private ModuleService $moduleService;
/**
* moduleTest constructor.
* @param ModuleService $moduleService
*/
public function __construct(ModuleService $moduleService)
{
$this->moduleService = $moduleService;
}
public function testModule()
{
$modules = $this->moduleService->getAllModules();
}
}
phpunit 尝试测试构造方法并崩溃
PHP 致命错误:未捕获的 ArgumentCountError:函数 App\Tests\moduleTest::__construct() 的参数太少,在第 138 行的 /vendor/phpunit/phpunit/src/Framework/TestBuilder.php 中传递了 0,而预期的正是 1在 /tests/moduleTest.php:20
更新
试着这样做
public function setUp(ModuleService $moduleService) : void
{
$this->moduleService = $moduleService;
}
但是现在我收到了这个错误:
ArgumentCountError: 函数 App\Tests\moduleTest::setUp() 的参数太少,在第 1126 行的 /vendor/phpunit/phpunit/src/Framework/TestCase.php 中传递了 0,而预期正好是 1
【问题讨论】:
-
我认为您正在寻找的是 SetUp 函数而不是构造函数。
-
@DirkScholten 你能给我举个例子吗?我是 phpunit 的新手