【问题标题】:testing in Laravel 5.5 : Class env does not exist在 Laravel 5.5 中进行测试:类 env 不存在
【发布时间】:2018-02-13 12:06:58
【问题描述】:

我正在 laravel 中开始测试

我用这个创建了一个“.env.testing”文件

APP_NAME=myApp
APP_ENV=testing
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://myApp.localhost

DB_CONNECTION=sqlite_testing

我在 config/database.php 文件的“连接”部分添加了这个

...
'sqlite_testing' => [
    'driver'   => 'sqlite',
    'database' => ':memory:',
    'prefix'   => '',
],
...

在 phpunit.xml 文件中,我添加了:

<env name="DB_CONNECTION" value="sqlite_testing" />

我创建了一个 UserTest 功能:

class UserTest extends TestCase
{

    public function setUp()
    {
        parent::setUp();
        Artisan::call('migrate');
        Artisan::call('db:seed');
    }

    public function tearDown()
    {
        parent::tearDown();
        Artisan::call('migrate:reset');
    }


    public function testCanAccessUserSpace()
    {
        $user = factory(User::class)->create();

        $response = $this->actingAs($user)
            ->get('/home');


        $response->assertStatus(200);
    }
}

但是当我运行测试时,我有这个:

ReflectionException: Class env does not exist

我的配置有什么问题?

谢谢

【问题讨论】:

  • 我们需要一个堆栈跟踪来找出哪里出了问题。我最初猜想您应该在完成自己的工作后调用parent::tearDown(),以便在您尝试 Artisan 调用时父级不会破坏任何应用程序实例。
  • 非常感谢!正如你所说,parent::tearDown(); 必须在Artisan::call('migrate:reset'); 之后调用

标签: laravel testing integration-testing


【解决方案1】:

我在运行 phpunit 时遇到了同样的错误:

ReflectionException:类 env 不存在

这是另一个问题,我已经安装了包 telescope 并且 phpunit 在测试时尝试加载它:

ReflectionException: Class env does not exist
/var/www/html/mat2/vendor/laravel/telescope/src/Telescope.php:263
/var/www/html/mat2/vendor/laravel/telescope/src/Telescope.php:222

等等。 我已经添加到 phpunit.xml:

<env name="TELESCOPE_ENABLED" value="false"/>

之后,测试运行良好。
所以,可能是包测试相关的错误。

【讨论】:

    【解决方案2】:

    就我而言,将 TELESCOPE_ENABLED 设置为 false 并不能解决我的问题。我必须发布

    php 工匠配置:清除

    我使用 Laravel 7。

    【讨论】:

      【解决方案3】:

      如果您使用的是php artisan test --env=testing,请将TELESCOPE_ENABLED=false 添加到您的.env.testing 文件并运行php artisan config:cache --env=testing

      【讨论】:

        【解决方案4】:

        似乎由于多种原因引发了此异常。在您的情况下,问题出在 tearDown 函数上,按照此函数中的特定操作顺序。您应该在底部调用父级 tearDown 。所以这个:

        public function tearDown()
        {
            parent::tearDown();
            Artisan::call('migrate:reset');
        }
        

        应该重构如下:

        public function tearDown()
        {
            Artisan::call('migrate:reset');
            parent::tearDown();
        }
        

        我怀疑原因是应用程序实例在Illuminate\Foundation\Testing\TestCase 内部被破坏。这是来自 Laravel 5.5 的代码:

        if ($this->app) {
            foreach ($this->beforeApplicationDestroyedCallbacks as $callback) {
                call_user_func($callback);
            }
        
            $this->app->flush();
        
            $this->app = null;
        }
        

        【讨论】:

          【解决方案5】:

          此错误可能是由于在单元测试环境中打开了望远镜。

          我将望远镜的默认行为更改为关闭并在我的.env 文件中明确打开它。

          您可以通过更新config/telescope.php 文件将望远镜的默认值更改为 false。

              /*
              |--------------------------------------------------------------------------
              | Telescope Master Switch
              |--------------------------------------------------------------------------
              |
              | This option may be used to disable all Telescope watchers regardless
              | of their individual configuration, which simply provides a single
              | and convenient way to enable or disable Telescope data storage.
              |
              */
          
              'enabled' => env('TELESCOPE_ENABLED', false), // Change from true to false here.
          

          然后您必须在 .env 文件中设置:TELESCOPE_ENABLED=true。这将确保您不会在您不想要的环境(例如生产和单元测试)中意外打开它。

          【讨论】:

            【解决方案6】:

            回复可能有点晚,但 phpunit.xml 中的以下行解决了问题。

            <server name="TELESCOPE_ENABLED" value="false"/>
            

            【讨论】:

              【解决方案7】:

              我遇到了这个问题,并在配置文件夹 > 常量文件中发现了问题 我在配置文件中写了“asset('/')”函数,我们不能在配置中写这样的函数。

              希望这个答案能帮助那些陷入这种错误的人。

              【讨论】:

                【解决方案8】:

                您需要将.env.testing 重命名为.env,您已经将APP_ENV 设置为testing,因此无需将您的.env 更改为.env.testing

                编辑:

                也尝试使用composer update 更新您的依赖关系,然后重试,这不是常见的错误,所以它与您的测试环境设置有关

                【讨论】:

                • .env 文件是我的开发环境,带有一个 mysql 数据库,所以我无法替换它
                • 尝试按以下顺序运行这些方法:composer updatecomposer dump-autoloadphp artisan clear:routephp artisan clear:configphp artisan optimize
                • id did : composer update, composer dump-autoload, php artisan route:clear, php artisan config:clear, php artisan optimize 我有同样的信息:ReflectionException: Class env does not exist
                • 我安装了新的 laravel 5.5 应用程序,我的测试正在运行,没有给我任何错误
                【解决方案9】:

                我的 config/services.php 文件中有同样的问题,我的错误是调用数组中的 route() 方法。我刚刚删除了该方法并重新开始工作。

                【讨论】:

                • 您能否通过分享一些代码更详细地解释您的答案。
                • 我在 de config 类中使用了一个辅助函数,这破坏了我的代码,这是一个发生在我身上的一般错误,没有代码可以共享。
                • 对我来说也一样,在我的情况下,这是因为我在 fortify.php 配置文件中使用了 route()
                • 很好!一个简单的愚蠢错误哈哈哈
                【解决方案10】:

                对于 laravel 版本 8,将 app()->environment() 更改为 env('ENV_NAME') 之类的

                env('SMS_SERVICE_ENDPOINT')
                

                例子:

                上一页:app()->environment() === 'production'

                新格式应该是 env('DEV_ENV') === 'production')

                注意: 不要忘记在您的 .env 文件中添加 DEV_ENV=production

                【讨论】:

                  【解决方案11】:

                  在 laravel 8 中,TestCase::tearDown 之后不要尝试使用 laravel 全局辅助函数或 Facade:

                  class MyTestCase extends TestCase
                  {
                    public function tearDown(): void
                    {
                      parent::tearDown();
                      // This will case this exception:  Target class [env] does not exist
                      echo app()->environment();
                    }
                  }
                  

                  相反,移动方法BEFORE parent::tearDown,它会起作用:

                  public function tearDown(): void
                  {
                     echo app()->environment();
                     parent::tearDown();
                  }
                  

                  【讨论】:

                    猜你喜欢
                    • 2020-03-17
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2018-05-06
                    • 1970-01-01
                    • 1970-01-01
                    • 2017-03-31
                    • 2018-02-13
                    相关资源
                    最近更新 更多