ReflectionException:类env不存在。难道我做错了什么?

时间:2019-03-26 11:10:00

标签: laravel phpunit

我目前正在尝试设置phpunit测试来测试我的API。问题是我无法以某种方式无法两次发送完全相同的请求。

我试图修改phpunit.xml来查看是否存在问题而没有成功,所以我现在怀疑自己以为我在代码本身中做错了事。

public function test_can_register_user() {
        $this->consoleLog('test_can_register_user');
        $generatedUser = str_random(5) . '@example.com';

        $data = [
            'name' => 'Test User',
            'email' => $generatedUser,
            'password' => 'test',
        ];

        var_dump($data);

        $expect = [
            'success' => true,
            'message' => 'Thanks for signing up! Please check your email to complete your registration.',
        ];

       $request = $this->post('/api/auth/register', $data)
            ->assertStatus(200)
            ->assertJson($expect);

       return $generatedUser;
    }

    /**
     * @depends test_can_register_user
     * @param $generatedUser
     */
    public function test_can_not_register_same_email($generatedUser) {
        $this->consoleLog('test_can_not_register_same_email');

        $data = [
            'name' => 'Test User',
            'email' => $generatedUser,
            'password' => 'test',
        ];

        var_dump($data);

        $expect = [
            'success' => false,
            "error" => [
                "email" => [
                "The email has already been taken."
                ]
            ]
            ];

       $request = $this->post('/api/auth/register', $data);
       var_dump($request->json());
       $request->assertStatus(200);
    }
     <php>
        <env name="DB_CONNECTION" value="mysql"/>
        <env name="APP_DEBUG" value="false"/>
        <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="MAIL_DRIVER" value="array"/>
        <env name="TELESCOPE_ENABLED" value="false"/>
    </php>

预期结果是将状态恢复为200。控制台中的实际结果如下:

Running test:
test_can_not_register_same_email
array(3) {
  ["name"]=>
  string(9) "Test User"
  ["email"]=>
  string(17) "C4v67@example.com"
  ["password"]=>
  string(4) "test"
}

ReflectionException : Class env does not exist
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:779
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:658
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:609
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:759
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:1222
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:519
 D:\Yeagger\yeagger-api\app\Providers\TelescopeServiceProvider.php:24
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Support\HigherOrderCollectionProxy.php:60
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Support\HigherOrderCollectionProxy.php:60
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Support\Collection.php:455
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Support\HigherOrderCollectionProxy.php:61
 D:\Yeagger\yeagger-api\vendor\laravel\telescope\src\Telescope.php:263
 D:\Yeagger\yeagger-api\vendor\laravel\telescope\src\Telescope.php:222
 D:\Yeagger\yeagger-api\vendor\laravel\telescope\src\Telescope.php:270
 D:\Yeagger\yeagger-api\vendor\laravel\telescope\src\Telescope.php:294
 D:\Yeagger\yeagger-api\vendor\laravel\telescope\src\Watchers\CacheWatcher.php:45
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php:360
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php:209
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Cache\Repository.php:497
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Cache\Repository.php:107
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Cache\RateLimiter.php:82
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Cache\RateLimiter.php:39
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php:52
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:151
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:104
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Routing\Router.php:684
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Routing\Router.php:659
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Routing\Router.php:625
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Routing\Router.php:614
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:176
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:30
 D:\Yeagger\yeagger-api\vendor\barryvdh\laravel-cors\src\HandleCors.php:36
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:151
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
 D:\Yeagger\yeagger-api\vendor\fideloper\proxy\src\TrustProxies.php:57
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:151
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php:31
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:151
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php:31
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:151
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php:27
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:151
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php:62
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:151
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:104
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:151
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:116
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:345
 D:\Yeagger\yeagger-api\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:195
 D:\Yeagger\yeagger-api\tests\Unit\UserTest.php:67

2 个答案:

答案 0 :(得分:1)

我通过以下命令解决了这个问题:

php artisan clear
php artisan config:clear

答案 1 :(得分:0)

对我来说,在设置PHPUnit之后,它已修复

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