Laravel 5.6-在phpunit测试中无法解析模型

时间:2018-09-07 20:46:18

标签: php laravel phpunit

我在单元测试中遇到问题,并且无法在控制器中正确解析用户模型。它确实提到它是一个字符串,但是我只能调用“ / api / v1 / users / 1”路由。它应该只解决正确的用户模型。我正在使用带有API资源,资源控制器和资源路由的Laravel 5.6.38。

单元测试

/** @test */
public function getting_specific_user()
{
    // Without Authentication
    $this->getJson('/api/v1/users/12345')
        ->assertStatus(401);

    // Create User and Auth Headers
    $this->createUserWithToken();

    // With Authentication
    $this->getJson('/api/v1/users/'.$this->user->id);
        ->assertOk()
        ->assertJsonFragment([
            'id' => $this->user->id,
            'name' => $this->user->name,
            'email' => $this->user->email,
            'active' => $this->user->active
        ]);

    // Get Undefined User
    $this->getJson('/api/v1/users/13232323')
        ->assertStatus(404);
}

堆栈跟踪

[2018-09-07 20:17:41] testing.ERROR: Argument 1 passed to App\Http\Controllers\API\UserController::show() must be an instance of App\User, string given {"userId":1,"email":"mcummerata@example.com","exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Argument 1 passed to App\\Http\\Controllers\\API\\UserController::show() must be an instance of App\\User, string given at /var/www/iosgen/iosgen_paper/app/Http/Controllers/API/UserController.php:58)
[stacktrace]
#0 [internal function]: App\\Http\\Controllers\\API\\UserController->show('testing', Object(Illuminate\\Http\\Request), Object(App\\User))
#1 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)
#2 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction('show', Array)
#3 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Route.php(212): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\API\\UserController), 'show')
#4 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Route.php(169): Illuminate\\Routing\\Route->runController()
#5 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Router.php(665): Illuminate\\Routing\\Route->run()
#6 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#7 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#8 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#9 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#10 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(43): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#11 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Auth\\Middleware\\Authenticate->handle(Object(Illuminate\\Http\\Request), Object(Closure), 'api')
#12 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#13 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(57): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#14 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Routing\\Middleware\\ThrottleRequests->handle(Object(Illuminate\\Http\\Request), Object(Closure), 60, '1')
#15 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#16 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#17 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Router.php(667): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#18 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Router.php(642): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#19 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Router.php(608): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#20 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Router.php(597): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#21 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#22 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#23 /var/www/iosgen/iosgen_paper/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#24 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Fideloper\\Proxy\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#25 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#26 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(31): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#27 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#28 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#29 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(31): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#30 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#31 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#32 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#33 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#34 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#35 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(62): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#36 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#37 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#38 /var/www/iosgen/iosgen_paper/vendor/hyn/multi-tenant/src/Middleware/EagerIdentification.php(29): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#39 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Hyn\\Tenancy\\Middleware\\EagerIdentification->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#40 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#41 /var/www/iosgen/iosgen_paper/vendor/hyn/multi-tenant/src/Middleware/HostnameActions.php(76): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#42 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Hyn\\Tenancy\\Middleware\\HostnameActions->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#43 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#44 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#45 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#46 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#47 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php(345): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#48 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php(317): Illuminate\\Foundation\\Testing\\TestCase->call('GET', 'http://testing....', Array, Array, Array, Array, '[]')
#49 /var/www/iosgen/iosgen_paper/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php(180): Illuminate\\Foundation\\Testing\\TestCase->json('GET', 'http://testing....', Array, Array)
#50 /var/www/iosgen/iosgen_paper/tests/Traits/InteractsWithPassport.php(47): Illuminate\\Foundation\\Testing\\TestCase->getJson('http://testing....', Array)
#51 /var/www/iosgen/iosgen_paper/tests/Endpoints/UsersTest.php(46): Tests\\Endpoints\\UsersTest->getJson('/api/v1/users/1')
#52 /var/www/iosgen/iosgen_paper/vendor/phpunit/phpunit/src/Framework/TestCase.php(1150): Tests\\Endpoints\\UsersTest->getting_specific_user()
#53 /var/www/iosgen/iosgen_paper/vendor/phpunit/phpunit/src/Framework/TestCase.php(844): PHPUnit\\Framework\\TestCase->runTest()
#54 /var/www/iosgen/iosgen_paper/vendor/phpunit/phpunit/src/Framework/TestResult.php(665): PHPUnit\\Framework\\TestCase->runBare()
#55 /var/www/iosgen/iosgen_paper/vendor/phpunit/phpunit/src/Framework/TestCase.php(798): PHPUnit\\Framework\\TestResult->run(Object(Tests\\Endpoints\\UsersTest))
#56 /var/www/iosgen/iosgen_paper/vendor/phpunit/phpunit/src/Framework/TestSuite.php(750): PHPUnit\\Framework\\TestCase->run(Object(PHPUnit\\Framework\\TestResult))
#57 /var/www/iosgen/iosgen_paper/vendor/phpunit/phpunit/src/Framework/TestSuite.php(750): PHPUnit\\Framework\\TestSuite->run(Object(PHPUnit\\Framework\\TestResult))
#58 /var/www/iosgen/iosgen_paper/vendor/phpunit/phpunit/src/Framework/TestSuite.php(750): PHPUnit\\Framework\\TestSuite->run(Object(PHPUnit\\Framework\\TestResult))
#59 /var/www/iosgen/iosgen_paper/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(587): PHPUnit\\Framework\\TestSuite->run(Object(PHPUnit\\Framework\\TestResult))
#60 /var/www/iosgen/iosgen_paper/vendor/phpunit/phpunit/src/TextUI/Command.php(203): PHPUnit\\TextUI\\TestRunner->doRun(Object(PHPUnit\\Framework\\TestSuite), Array, true)
#61 /var/www/iosgen/iosgen_paper/vendor/phpunit/phpunit/src/TextUI/Command.php(159): PHPUnit\\TextUI\\Command->run(Array, true)
#62 /var/www/iosgen/iosgen_paper/vendor/phpunit/phpunit/phpunit(53): PHPUnit\\TextUI\\Command::main()
#63 {main}
"} 

我的主应用程序无法在测试中工作。知道是什么原因造成的吗? 谢谢!

编辑

在这里使用InteractsWithPassport特性

trait InteractsWithPassport
{

    protected $headers = [];
    protected $scopes = [];
    protected $user;
    protected $baseUrl = 'http://testing.com';

    public function createUserWithToken()
    {

        $clientRepository = new ClientRepository();
        $client = $clientRepository->createPersonalAccessClient(
            null, 'Test Personal Access Client', $this->baseUrl
        );

        DB::table('oauth_personal_access_clients')->insert([
            'client_id' => $client->id,
            'created_at' => new DateTime,
            'updated_at' => new DateTime,
        ]);

        $this->user = factory(User::class)->create();
        $token = $this->user->createToken('TestToken', $this->scopes)->accessToken;
        $this->headers['Accept'] = 'application/json';
        $this->headers['Content-Type'] = 'application/json';
        $this->headers['Authorization'] = 'Bearer '.$token;

    }

    public function get($uri, array $headers = [])
    {
        return parent::get($this->addBaseUrl($uri), array_merge($this->headers, $headers));
    }

    public function getJson($uri, array $headers = [])
    {
        return parent::getJson($this->addBaseUrl($uri), array_merge($this->headers, $headers));
    }

    public function postJson($uri, array $data = [], array $headers = [])
    {
        return parent::postJson($this->addBaseUrl($uri), $data, array_merge($this->headers, $headers));
    }

    public function putJson($uri, array $data = [], array $headers = [])
    {
        return parent::putJson($this->addBaseUrl($uri), $data, array_merge($this->headers, $headers));
    }

    public function deleteJson($uri, array $data = [], array $headers = [])
    {
        return parent::deleteJson($this->addBaseUrl($uri), $data, array_merge($this->headers, $headers));
    }

    protected function addBaseUrl($uri)
    {
        return $this->baseUrl . $uri;
    }
}

0 个答案:

没有答案