我遇到了与许多其他类似问题相同的问题,但没有一个解决方案奏效。
我有一个非常简单的测试用例:
class ExampleTest extends TestCase
{
public function test_find_english()
{
$this->withoutExceptionHandling();
$response = $this->get("api/find?id=1&language=eng");
$response->assertOk();
}
}
如果我在浏览器或Postman中访问端点,我会得到预期的结果,但如果我运行测试,我会获得:
PHPUnit 9.5.2 by Sebastian Bergmann and contributors.
SymfonyComponentHttpKernelExceptionNotFoundHttpException : GET http://localhost/api/find
我试过了:
- 指定带或不带前导
/
的URL - 有或没有
api/
- 完整指定URL,包括
http://
部分 - 指定可在我的浏览器中使用的URL,以及
http://localhost
和http://127.0.0.1
,无论是否使用:8000
我还在.env和.env.testing文件以及phpunit.xml文件中尝试了上述操作,注意到异常中出现的URL不会反映任何更改,除非我将其正确地放在get((方法中中,即使每次更改后我都运行了php artisan config:cache
和php artisan cache:clear
这是我的phpunit.xml:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
processIsolation="true"
>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
<php>
<server name="APP_ENV" value="testing"/>
<!-- <server name="BCRYPT_ROUNDS" value="4"/>-->
<!-- <server name="CACHE_DRIVER" value="array"/>-->
<!-- <server name="DB_CONNECTION" value="sqlite"/> -->
<!-- <server name="DB_DATABASE" value=":memory:"/> -->
<!-- <server name="MAIL_MAILER" value="array"/>-->
<!-- <server name="QUEUE_CONNECTION" value="sync"/>-->
<!-- <server name="SESSION_DRIVER" value="array"/>-->
<!-- <server name="TELESCOPE_ENABLED" value="false"/>-->
</php>
</phpunit>
还有我的.env.testing:
APP_NAME=MyApp
APP_ENV=testing
APP_KEY=# redacted
APP_DEBUG=true
APP_URL=http://localhost
.env与APP_ENV=local
不同。在我添加.env.testing.之前,问题就已经存在了
我在一个模糊的实例(Laravel Homestead(上运行这个。
我设法找到了这个问题。我没有提到我正在通过PHP Storm运行测试,这是一个致命的细节:我以为我已经正确配置了我的解释器,因为它在其他方面都能工作,但实际上我在同一个实例上使用了不同Laravel项目的设置。
如果您最终来到这里并使用IDE运行测试,请确保您的解释器对于项目是正确的。我的指向的是另一个项目的配置文件,所以它当然永远不会工作。