【问题标题】:Laravel Tests in custom directory - Getting error "the name is already in use"自定义目录中的 Laravel 测试 - 出现错误“名称已在使用中”
【发布时间】:2017-10-27 13:01:56
【问题描述】:

我正在尝试构建一个使用 laravel 的应用程序。有一个测试目录,我用它来测试应用程序。

但我在 app/MyApp/Service 目录中有一个服务。这是目录结构。

app/MyApp/Service 
|-->Services 
|-->Interfaces 
|-->Test 
|------->OLSServiceTest.php

这里是 OLSServiceTest.php 内容

<?php

namespace App\MyApp\Service\Test;

class OLSServiceTest extends TestCase
{

    public function testInfo()
    {
        $this->assertTrue(true);
    }

    public function testLogin()
    {
        $this->assertTrue(false);
    }

    /**
     * Creates the application.
     *
     * Needs to be implemented by subclasses.
     *
     * @return \Symfony\Component\HttpKernel\HttpKernelInterface
     */
    public function createApplication()
    {
        // TODO: Implement createApplication() method.
    }
}

然后我尝试手动运行测试

$ vendor/phpunit/phpunit/phpunit App\\MyApp\\Service\\Test\\OLSServiceTest

但是得到这个错误:

[Symfony\Component\Debug\Exception\FatalErrorException]
无法声明类 App\MyApp\Service\Test\OLSServiceTest,因为 该名称已被使用

致命错误:无法声明类 App\MyApp\Service\Test\OLSServiceTest,因为名字已经在 在第 29 行的 /var/www/app/MyApp/Service/Test/OLSServiceTest.php 中使用

我做错了什么?

【问题讨论】:

  • 您在该命名空间中还有其他同名的类吗?您可以尝试使用类的不同名称吗?
  • 没办法,当我更改名称时,我收到新名称的错误。
  • 您是否尝试过使用路径而不是命名空间来运行测试?供应商/phpunit/phpunit/phpunit app/MyApp/Service/Test/OLSServiceTest.php ?
  • 当我尝试使用路径时工作,比你非常多@shivanshupatel
  • 酷我已经发布了同样的事情它的答案你可以将它标记为正确,如果它有效:) 祝你好运

标签: php laravel lumen


【解决方案1】:

似乎您正在尝试使用错误的命令运行测试。尝试使用 Vendor bin 目录中的 PHPUnit,我认为它会解决您的问题。

vendor/bin/phpunit app/MyApp/Service/Test/OLSServiceTest.php

【讨论】:

    猜你喜欢
    • 2015-08-10
    • 2021-02-08
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 2018-06-04
    • 1970-01-01
    相关资源
    最近更新 更多