小编Maz*_*Maz的帖子

调用未定义的方法 Illuminate\Container\Container::getLocale() | 拉拉维尔 8

单元测试对我来说比功能测试更困难,我正在尝试编写与我的中间件相关的单元测试,但是出了什么问题。有人可以帮我修复这个错误,或者为这个中间件编写一个正常的单元测试。

我的中间件

<?php

namespace App\Http\Middleware;

use App\Services\SettingsService;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

final class Locale
{
    private SettingsService $settingsService;

    public function __construct(SettingsService $settingsService)
    {
        $this->settingsService = $settingsService;
    }

    public function handle(Request $request, Closure $next): mixed
    {
        $locale = $this->settingsService->get('language', app()->getLocale());
        app()->setLocale($locale);

        return $next($request);
    }
}

Run Code Online (Sandbox Code Playgroud)

我的测试

<?php

namespace Tests\Unit;

use App\Http\Middleware\Locale;
use App\Services\SettingsService;
use Illuminate\Http\Request;
use PHPUnit\Framework\TestCase;

class LocaleMiddlewareTest extends TestCase
{
    /**
     * A basic unit test example.
     *
     * @return void
     */
    public function test_example()
    {
        $request …
Run Code Online (Sandbox Code Playgroud)

phpunit laravel

2
推荐指数
1
解决办法
1771
查看次数

标签 统计

laravel ×1

phpunit ×1