在Laravel测试单元中的__ -Construct无法填充属性

发布于 2025-02-03 03:00:27 字数 1917 浏览 1 评论 0 原文

我创建了一个这样的简单测试:

namespace Tests\Feature;

use Tests\TestCase;
use App\Models\User;
use Illuminate\Support\Facades\Config;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class simpleTest extends TestCase
{
    public $form_array = ['forms_data'];


    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_example()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

上面的测试没有问题。 我有一个公共属性$ form_array,在上面的示例中具有静态值,但是当我尝试用 __ construct()填充$ form_array时,我会得到此错误:

  array_merge(): Argument #1 must be of type array, null given

  at vendor/phpunit/phpunit/phpunit:98
     94▕ unset($options);
     95▕ 
     96▕ require PHPUNIT_COMPOSER_INSTALL;
     97▕ 
  ➜  98▕ PHPUnit\TextUI\Command::main();
     99▕ 

代码:

namespace Tests\Feature;

use Tests\TestCase;
use App\Models\User;
use Illuminate\Support\Facades\Config;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class simpleTest extends TestCase
{
    public $form_array;


    public function __construct(){

        $this->form_array = ['forms_data'];

    }


    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_example()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
 }

我的目标是在laravel中使用配置文件中的值填充$ form_data,我找到了这种方式:

public function __construct(){
    parent::setUp();
    $this->form_array = Config::get('ravandro.form_array');
}

但是,即使是简单的 $ this-form_data ='value'不起作用,我真的很困惑!

顺便说一句,我使用Laravel Octane为我的应用服务,但我认为这对测试没有任何影响。

PHP版本:8.1
Laravel:9
php-unit:“ phpunit/phpunit”:“^9.3.3”

I have created a simple test like this :

namespace Tests\Feature;

use Tests\TestCase;
use App\Models\User;
use Illuminate\Support\Facades\Config;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class simpleTest extends TestCase
{
    public $form_array = ['forms_data'];


    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_example()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

The test above runs without a problem .
I have a public property $form_array which in the example above has a static value, but when I'm trying to fill $form_array with __construct() in test, I get this error :

  array_merge(): Argument #1 must be of type array, null given

  at vendor/phpunit/phpunit/phpunit:98
     94▕ unset($options);
     95▕ 
     96▕ require PHPUNIT_COMPOSER_INSTALL;
     97▕ 
  ➜  98▕ PHPUnit\TextUI\Command::main();
     99▕ 

code :

namespace Tests\Feature;

use Tests\TestCase;
use App\Models\User;
use Illuminate\Support\Facades\Config;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class simpleTest extends TestCase
{
    public $form_array;


    public function __construct(){

        $this->form_array = ['forms_data'];

    }


    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_example()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
 }

My goal is fill the $form_data with values from config files in laravel which i found this way :

public function __construct(){
    parent::setUp();
    $this->form_array = Config::get('ravandro.form_array');
}

but even a simple $this->form_data = 'value' doesnt work , and im really confused !

btw , Im using Laravel Octane to serve my application but I dont think it has any effect to tests.

Php version : 8.1
Laravel : 9
php-unit: "phpunit/phpunit": "^9.3.3"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_畞蕅 2025-02-10 03:00:27

在测试中,正确的构造方法被称为 setup(),该方法在该类上的每个测试之前运行。

您可以在

In tests the correct constructor method is called setUp(), which runs before every test on that class.

You can check an example of this on https://phpunit.readthedocs.io/en/9.5/fixtures.html#fixtures

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文