gpt4 book ai didi

php - Laravel - 尚未设置外观根

转载 作者:行者123 更新时间:2023-12-03 20:21:15 25 4
gpt4 key购买 nike

我在 Laravel 应用程序中运行测试时遇到问题。
我的应用程序被拆分为单独的命名空间。 Laravel App 命名空间在 app 目录中,它是 App/命名空间。我在 src 目录中有额外的命名空间。

我的测试用例看起来像这样:

<?php

namespace Tests\Unit;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use PHPUnit\Framework\TestCase;
use SmoothCode\Sample\Domain\User\User;
use SmoothCode\Sample\Domain\User\UserRepository;
use SmoothCode\Sample\Domain\User\ValueObject\ConfirmationCode;
use SmoothCode\Sample\Shared\ValueObjects\Email;
use SmoothCode\Sample\Shared\ValueObjects\Id;
use SmoothCode\Sample\Shared\ValueObjects\Password;
use Tests\CreatesApplication;


class UserDomainTest extends TestCase
{
use CreatesApplication;

protected UserRepository $userRepository;

public function testUserCreation() {
$user = User::create(
Id::generate(),
'Jan',
'Kowalski',
new Email('test@test.com'),
'123123123',
new Password('Pass123!'),
new \DateTimeImmutable(),
ConfirmationCode::generate()
);
//
// $this->assertInstanceOf(User::class, $user);
}

protected function setUp(): void
{
parent::setUp();
}


}

运行 vendor/bin/phpunit 后,我​​收到以下错误:
1) Tests\Unit\UserDomainTest::testUserCreation
RuntimeException: A facade root has not been set.

/home/jakub/Development/Projects/streetboss-server/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:258
/home/jakub/Development/Projects/streetboss-server/src/Sample/Shared/ValueObjects/Password.php:15
/home/jakub/Development/Projects/streetboss-server/tests/Unit/UserDomainTest.php:29

从那我知道问题出在 src/Sample/Shared/ValueObjects/Password.php:15

看起来像:

<?php

namespace SmoothCode\Sample\Shared\ValueObjects;

use Illuminate\Support\Facades\Hash;
use Webmozart\Assert\Assert;

class Password {
protected string $hash;

public function __construct($plainPassword)
{
Assert::minLength($plainPassword, 6);

$this->hash = Hash::make($plainPassword);
}

public function hashedPassword()
{
return $this->hash;
}


}
I was trying to run:
php artisan config:cache
php artisan cache:clear
php artisan config:clear
composer dump-autoload

但我仍然收到此错误。

最佳答案

好的,我已经找到了解决此错误的方法。对于任何有同样问题的人:

我的 UserDomainTest 正在从命名空间扩展 TestCase:

use PHPUnit\Framework\TestCase;

当我改为:

use Illuminate\Foundation\Testing\TestCase;

一切都像一个魅力。

关于php - Laravel - 尚未设置外观根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60022588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com