iT邦幫忙

0

laravel test 問題

  • 分享至 

  • xImage

我想要在server網站去寫unit test測試client1,client2,client3...網站的api。
目前遇到的問題是執行php artisan test時發現程式可以抓到client的route,但route會去抓server的controller。

以下是我的code

server\tests\CreatesApplication.php

trait CreatesApplication
{
    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        // 研究後發現改這裡可以抓到目標網站
        $client = '/../client1';
        // $client = '';

        $app = require __DIR__ . '/..' . $client . '/bootstrap/app.php';

        $app->make(Kernel::class)->bootstrap();

        return $app;
    }
}

server\tests\Feature\LoginTest.php

class LoginTest extends TestCase
{
    private $uri = '/api/v1/user/login';
    /**
     * 測試管理員能登入
     *
     * @return void
     */
    public function test_admin_can_login()
    {
        $data = [
            'email' => 'amdin@localhost',
            'password' => 'adminpass',
        ];

        $headers = [
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
        ];

        $response = $this->post($this->uri, $data, $headers);

        $response->assertOk();
    }
}

client1\routes\api.php

Route::post('/v1/user/login', [UserController::class, 'login']);

clinet1\app\Http\Controllers\UserController.php

class UserController extends Controller
{
    public function login(Request $request)
    {
        return response('', 200);
    }
}

log檔:client1\storage\logs\laravel.log

[2021-10-25 06:48:09] testing.ERROR: Target class [App\Http\Controllers\UserController] does not exist. {"exception":"[object] (Illuminate\\Contracts\\Container\\BindingResolutionException(code: 0): Target class [App\\Http\\Controllers\\UserController] does not exist. at C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php:879)
[stacktrace]
#0 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(758): Illuminate\\Container\\Container->build('App\\\\Http\\\\Contro...')
#1 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php(841): Illuminate\\Container\\Container->resolve('App\\\\Http\\\\Contro...', Array, true)
#2 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(694): Illuminate\\Foundation\\Application->resolve('App\\\\Http\\\\Contro...', Array)
#3 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php(826): Illuminate\\Container\\Container->make('App\\\\Http\\\\Contro...', Array)
#4 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(276): Illuminate\\Foundation\\Application->make('App\\\\Http\\\\Contro...')
#5 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(1051): Illuminate\\Routing\\Route->getController()
#6 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(1012): Illuminate\\Routing\\Route->controllerMiddleware()
#7 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(708): Illuminate\\Routing\\Route->gatherMiddleware()
#8 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(688): Illuminate\\Routing\\Router->gatherRouteMiddleware(Object(Illuminate\\Routing\\Route))
#9 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(672): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#10 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(636): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#11 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(625): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#12 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(166): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#13 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(128): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#14 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#15 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull.php(31): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#16 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#17 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#18 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TrimStrings.php(40): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#19 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TrimStrings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#20 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#21 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#22 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance.php(86): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#23 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#24 C:\\xampp\\htdocs\\test\\server\\vendor\\fruitcake\\laravel-cors\\src\\HandleCors.php(52): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#25 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Fruitcake\\Cors\\HandleCors->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#26 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Http\\Middleware\\TrustProxies.php(39): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#27 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Http\\Middleware\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#28 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#29 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(141): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#30 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(110): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#31 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Testing\\Concerns\\MakesHttpRequests.php(510): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#32 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Testing\\Concerns\\MakesHttpRequests.php(322): Illuminate\\Foundation\\Testing\\TestCase->call('POST', '/api/v1/user/lo...', Array, Array, Array, Array)
#33 C:\\xampp\\htdocs\\test\\server\\tests\\Feature\\LoginTest.php(29): Illuminate\\Foundation\\Testing\\TestCase->post('/api/v1/user/lo...', Array, Array)
#34 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\Framework\\TestCase.php(1527): Tests\\Feature\\LoginTest->test_admin_can_login()
#35 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\Framework\\TestCase.php(1133): PHPUnit\\Framework\\TestCase->runTest()
#36 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\Framework\\TestResult.php(722): PHPUnit\\Framework\\TestCase->runBare()
#37 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\Framework\\TestCase.php(885): PHPUnit\\Framework\\TestResult->run(Object(Tests\\Feature\\LoginTest))
#38 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\Framework\\TestSuite.php(678): PHPUnit\\Framework\\TestCase->run(Object(PHPUnit\\Framework\\TestResult))
#39 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\Framework\\TestSuite.php(678): PHPUnit\\Framework\\TestSuite->run(Object(PHPUnit\\Framework\\TestResult))
#40 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\Framework\\TestSuite.php(678): PHPUnit\\Framework\\TestSuite->run(Object(PHPUnit\\Framework\\TestResult))
#41 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\TextUI\\TestRunner.php(670): PHPUnit\\Framework\\TestSuite->run(Object(PHPUnit\\Framework\\TestResult))
#42 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\TextUI\\Command.php(143): PHPUnit\\TextUI\\TestRunner->run(Object(PHPUnit\\Framework\\TestSuite), Array, Array, true)
#43 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\TextUI\\Command.php(96): PHPUnit\\TextUI\\Command->run(Array, true)
#44 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\phpunit(92): PHPUnit\\TextUI\\Command::main()
#45 {main}

[previous exception] [object] (ReflectionException(code: -1): Class App\\Http\\Controllers\\UserController does not exist at C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php:877)
[stacktrace]
#0 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(877): ReflectionClass->__construct('App\\\\Http\\\\Contro...')
#1 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(758): Illuminate\\Container\\Container->build('App\\\\Http\\\\Contro...')
#2 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php(841): Illuminate\\Container\\Container->resolve('App\\\\Http\\\\Contro...', Array, true)
#3 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(694): Illuminate\\Foundation\\Application->resolve('App\\\\Http\\\\Contro...', Array)
#4 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php(826): Illuminate\\Container\\Container->make('App\\\\Http\\\\Contro...', Array)
#5 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(276): Illuminate\\Foundation\\Application->make('App\\\\Http\\\\Contro...')
#6 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(1051): Illuminate\\Routing\\Route->getController()
#7 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(1012): Illuminate\\Routing\\Route->controllerMiddleware()
#8 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(708): Illuminate\\Routing\\Route->gatherMiddleware()
#9 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(688): Illuminate\\Routing\\Router->gatherRouteMiddleware(Object(Illuminate\\Routing\\Route))
#10 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(672): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#11 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(636): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#12 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(625): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#13 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(166): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#14 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(128): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#15 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#16 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull.php(31): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#17 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#18 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#19 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TrimStrings.php(40): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#20 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TrimStrings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#21 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#22 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#23 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance.php(86): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#24 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#25 C:\\xampp\\htdocs\\test\\server\\vendor\\fruitcake\\laravel-cors\\src\\HandleCors.php(52): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#26 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Fruitcake\\Cors\\HandleCors->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#27 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Http\\Middleware\\TrustProxies.php(39): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#28 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Http\\Middleware\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#29 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#30 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(141): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#31 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(110): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#32 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Testing\\Concerns\\MakesHttpRequests.php(510): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#33 C:\\xampp\\htdocs\\test\\server\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Testing\\Concerns\\MakesHttpRequests.php(322): Illuminate\\Foundation\\Testing\\TestCase->call('POST', '/api/v1/user/lo...', Array, Array, Array, Array)
#34 C:\\xampp\\htdocs\\test\\server\\tests\\Feature\\LoginTest.php(29): Illuminate\\Foundation\\Testing\\TestCase->post('/api/v1/user/lo...', Array, Array)
#35 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\Framework\\TestCase.php(1527): Tests\\Feature\\LoginTest->test_admin_can_login()
#36 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\Framework\\TestCase.php(1133): PHPUnit\\Framework\\TestCase->runTest()
#37 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\Framework\\TestResult.php(722): PHPUnit\\Framework\\TestCase->runBare()
#38 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\Framework\\TestCase.php(885): PHPUnit\\Framework\\TestResult->run(Object(Tests\\Feature\\LoginTest))
#39 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\Framework\\TestSuite.php(678): PHPUnit\\Framework\\TestCase->run(Object(PHPUnit\\Framework\\TestResult))
#40 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\Framework\\TestSuite.php(678): PHPUnit\\Framework\\TestSuite->run(Object(PHPUnit\\Framework\\TestResult))
#41 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\Framework\\TestSuite.php(678): PHPUnit\\Framework\\TestSuite->run(Object(PHPUnit\\Framework\\TestResult))
#42 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\TextUI\\TestRunner.php(670): PHPUnit\\Framework\\TestSuite->run(Object(PHPUnit\\Framework\\TestResult))
#43 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\TextUI\\Command.php(143): PHPUnit\\TextUI\\TestRunner->run(Object(PHPUnit\\Framework\\TestSuite), Array, Array, true)
#44 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\src\\TextUI\\Command.php(96): PHPUnit\\TextUI\\Command->run(Array, true)
#45 C:\\xampp\\htdocs\\test\\server\\vendor\\phpunit\\phpunit\\phpunit(92): PHPUnit\\TextUI\\Command::main()
#46 {main}
"} 

有照官方文檔指示引用Controller了,也在server新增Controller是可以引抓的
研究好幾天找不到方法,跪求大佬幫忙,感謝QQ

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
ElkSong
iT邦新手 5 級 ‧ 2021-10-25 15:58:58

也許是
clinet1\app\Http\Controllers\UserController.php
UserController.php檔案名稱大小寫的問題
或者是namespace有誤

看更多先前的回應...收起先前的回應...
adha9990 iT邦新手 4 級 ‧ 2021-10-25 17:54:32 檢舉

剛檢查大小寫跟namespace沒問題
用postman對client1測試api也能正常response
我懷疑是底層可能做了什麼導致相對路徑抓到server這來?

ElkSong iT邦新手 5 級 ‧ 2021-10-25 22:14:50 檢舉

嘗試看看這段,我測試通過Unit Test

<?php

namespace Tests\Feature;

use App\Http\Controllers\LoginController;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Tests\TestCase;

class LoginControllerTest extends TestCase
{
    use RefreshDatabase;
    use WithoutMiddleware;

    protected function ajaxPost($uri, array $data = [])
    {
        return $this->post($uri, $data, array('HTTP_X-Requested-With' => 'XMLHttpRequest'));
    }

    public function test_login()
    {
        $response = $this->ajaxPost(action([LoginController::class, 'login']));
        $response->assertOk();
    }
}

adha9990 iT邦新手 4 級 ‧ 2021-10-26 05:28:40 檢舉

還是不行,所以我打算用網址的方式來測試,開兩個乾淨的新專案a,b
a\tests\Feature

public function test_example()
{
    $url = 'http://www.b.com/api/test/';

    $response = $this->get($url);

    $response->assertStatus(200);
}

b\routes\api.php

Route::get('/test', function () {
    return response('test', 200);
});

還是404...
https://ithelp.ithome.com.tw/upload/images/20211026/20122444yPQf56LNOL.png
但我在Postman卻又可以200
https://ithelp.ithome.com.tw/upload/images/20211026/20122444d68ngJCEol.png
是不是要加什麼header或是請求的方法有誤?

小魚 iT邦大師 1 級 ‧ 2021-10-26 13:02:59 檢舉

api/test/ 和 api/test 應該是不一樣的

1
小魚
iT邦大師 1 級 ‧ 2021-10-25 17:52:55

應該是要用網址來訪問吧.

adha9990 iT邦新手 4 級 ‧ 2021-10-25 18:54:35 檢舉

我這次再改成這樣還是有問題,但Postman呼叫卻抓得到
https://ithelp.ithome.com.tw/upload/images/20211025/20122444djTW9bZCsn.png
https://ithelp.ithome.com.tw/upload/images/20211025/20122444y91vNLvtpW.png
https://ithelp.ithome.com.tw/upload/images/20211025/201224440AP7aspQGC.png

小魚 iT邦大師 1 級 ‧ 2021-10-26 13:01:25 檢舉

API,
你用的是Get方法嗎?

我要發表回答

立即登入回答