【问题标题】:I'm getting this when doing a test: Function name must be a string我在做测试时得到这个:函数名必须是一个字符串
【发布时间】:2020-03-06 05:05:13
【问题描述】:

我在执行测试时遇到这个错误,我不知道如何解决这个问题,有什么想法吗?

1) 测试\功能\Http\Controllers\Auth\Editar_Pelicula::Editar_Pelicula Symfony\Component\Debug\Exception\FatalThrowableError: 函数名必须是字符串

<?php

namespace Tests\Feature\Http\Controllers\Auth;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
use App\User;
use App\Movie;

class Editar_Pelicula extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    /** @test */
    public function Editar_Pelicula(){
        $this->withoutExceptionHandling();
        $response = $this->withoutMiddleware()->put('catalog/edit/1', [
            'title' => 'testeo',
            'year' => '9999',
            'director' => 'Eduard',
            'synopsis' => 'blablabla',
            'category' => 3,
            'trailer' => 'https://www.youtube.com/embed/XXXXXX'
        ]);

        $this->assertDatabaseHas('movies', [
            'title' => 'testeo',
            'year' => '9999',
            'director' => 'Eduard',
            'synopsis' => 'hola',
        ]);
    }

}

【问题讨论】:

  • 我认为您不能在方法名称中使用下划线,您可以将 Editar_Pelicula 替换为 EditarPelicula 吗?
  • 我试过了,但仍然显示相同的错误信息
  • 由于您的类和方法都命名为Editar_Pelicula,因此该方法充当类的构造函数。这可能会覆盖框架需要做的构建。我建议您将 method 重命名为 testEditarPelicula - 这将使其成为常规实例方法,并且被命名为 test 可以使框架了解它是一个测试场景这一事实。
  • @bishop 您可能应该将其发布为答案

标签: php laravel function testing


【解决方案1】:

由于您的类和方法都命名为Editar_Pelicula,因此该方法充当类的构造函数。这不是你想做的。

要表明该方法是一个测试场景,请重命名该方法,使其以test 开头。然后,最佳做法是以描述正在测试的场景的方式命名方法。

例如,将方法重命名为test_A_Valid_Put_Changes_Database。

【讨论】:

    猜你喜欢
    • 2020-03-23
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-11
    • 1970-01-01
    • 2019-10-04
    相关资源
    最近更新 更多