我是新來的Codeception,我試圖測試我使用Laravel 5構建的Web服務。我遵循指南here。如何使用Codeception測試laravel 5 API?
因此,我創建了一套名爲API第一:
codecept generate:suite api
api.suite.yml
class_name: ApiTester
modules:
enabled:
- REST:
url: http://localhost:8000/api/
depends: Laravel5
AuthenticateUserCept.php
<?php
$I = new ApiTester($scenario);
$I->wantTo('authenticate a user');
$I->haveHttpHeader('Content-Type', 'application/x-www-form-urlencoded');
$I->sendPOST('/authenticate', [
'username' => 'archive',
'email' => '[email protected]',
'password' => 'password'
]);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
我已經嘗試這使用Post男人,它運行良好。 /authenticate
路由採用3個參數並高興地吐出一個JWT令牌,但我無法使其與Codeception一起工作。
1) Failed to authenticate a user in AuthenticateUserCept (tests/api//AuthenticateUserCept.php)
Step I see response code is 200
Fail Failed asserting that 500 matches expected 200.
Scenario Steps:
3. $I->seeResponseCodeIs(200)
2. $I->sendPOST("/authenticate",{"username":"archive","email":"[email protected]","password":"password"})
1. $I->haveHttpHeader("Content-Type","application/x-www-form-urlencoded")
我在哪裏出錯了?此外,對於我來說,我如何重構這個特定的測試,以便爲我提出的其他請求建立JWT。任何幫助表示讚賞。
在api.suite.yml
class_name: ApiTester
modules:
enabled:
- REST:
url: http://localhost:8000/api/
depends: Laravel5
config:
Laravel5:
environment_file: .env.testing
謝謝你的精彩見解。所以我這次繼續將POST請求改爲'/ api/authenticate'。所以我挖掘了測試的輸出。我想出了一箇中間件,它檢查令牌以評估有效負載是否搞亂了這些,但中間件不應該由於明顯的原因而對'/ api/authenticate'進行操作。但是當我評論Kernal的中間件時,它運行良好。我該如何解決這個問題? – Rohan
將這個到您的api.suite.yml: 配置: Laravel5: environment_file:.env.testing 在您的文件.env.testing,確保您已設置: APP_ENV =測試 否則應該和你的.env文件一樣。 默認情況下,當APP_ENV =測試中間件沒有運行時。您可以根據具體情況重新啓用它,但應該可以解決您的問題。 – delatbabel
我不確定我是否掌握了所有內容。我更改了配置文件,我甚至會編輯我的答案以包含更改。我創建了.env.testing並將'APP_ENV'設置爲測試。但是我不明白你對中間件的意思。測試仍然失敗。試圖解析它不應該使用的令牌的中間件。一旦我評論Kernal中的中間件,它就通過了,但這不是我想要解決的問題。中間件查看請求是否爲'api/authenticate',然後跳過。也許codeception不執行HTTP請求?它在另一箇中做到了。我很困惑。 – Rohan