lumen - 为什么 PHPUnit 注册测试 returns 405?
lumen - Why does PHPUnit register test returns 405?
我想为 lumen 应用程序进行 php 单元测试,例如:
public function testRegisterUser()
{
$newUserData = [
'name' => 'test_user',
'email' => 'test_user@mail.com',
'password' => Hash::make('111111'),
'status' => 'A',
'has_debts' => false
];
$response = $this->get('/api/v1/register', $newUserData); // http://localhost:8000/api/v1/register
$this->assertResponseOk();
}
但是 运行 测试我得到 405 错误:
1) PagesTest::testRegisterUser
Expected response status code [200] but received 405.
Failed asserting that 200 is identical to 405.
/ProjectPath/vendor/illuminate/testing/TestResponse.php:177
/ProjectPath/vendor/illuminate/testing/TestResponse.php:99
/ProjectPath/vendor/laravel/lumen-framework/src/Testing/Concerns/MakesHttpRequests.php:415
/ProjectPath/tests/PagesTest.php:27
为什么我得到 405 Method Not Allowed?我邮递员我检查我的方法:https://prnt.sc/207bu03
邮递员中的方法/api/v1/register没有任何令牌保护。
如何让我的测试工作?
更新块:
我收到错误:
Error: Call to undefined method PagesTest::getJson()
如果我修改:
$response = $this->getJson('/api/v1/register', $newUserData);
这里提到了 getJson 方法:https://laravel.com/docs/8.x/http-tests
但是在这个页面上,我在测试文件头中看到:
命名空间 Tests\Feature;
但我生成的流明测试文件中没有。
在我的 routes/web.php 我有 :
$router->group(['prefix'=>'api/v1'], function() use($router){
$router->post('/register','AuthController@register');
$router->post('/login', 'AuthController@login');
$router->group(['middleware' => 'auth'], function () use ($router) {
$router->get('/profile', 'UserProfileController@index');
怎么了?
谢谢!
因为您可能必须使用 $this->getJson
而不是 $this->get
。
它不是 http://localhost:8000
,因为你正在测试,你不是在字面上访问 URL。它正在模拟那个。
也请分享您的 api.php
或路由文件和控制器(还有正在处理 URL 的中间件)。
查看 Lumen's documentation 我可以看到没有 getJson
,我的错。您必须改用 $this->json('GET'
。
我想为 lumen 应用程序进行 php 单元测试,例如:
public function testRegisterUser()
{
$newUserData = [
'name' => 'test_user',
'email' => 'test_user@mail.com',
'password' => Hash::make('111111'),
'status' => 'A',
'has_debts' => false
];
$response = $this->get('/api/v1/register', $newUserData); // http://localhost:8000/api/v1/register
$this->assertResponseOk();
}
但是 运行 测试我得到 405 错误:
1) PagesTest::testRegisterUser
Expected response status code [200] but received 405.
Failed asserting that 200 is identical to 405.
/ProjectPath/vendor/illuminate/testing/TestResponse.php:177
/ProjectPath/vendor/illuminate/testing/TestResponse.php:99
/ProjectPath/vendor/laravel/lumen-framework/src/Testing/Concerns/MakesHttpRequests.php:415
/ProjectPath/tests/PagesTest.php:27
为什么我得到 405 Method Not Allowed?我邮递员我检查我的方法:https://prnt.sc/207bu03
邮递员中的方法/api/v1/register没有任何令牌保护。 如何让我的测试工作?
更新块: 我收到错误:
Error: Call to undefined method PagesTest::getJson()
如果我修改:
$response = $this->getJson('/api/v1/register', $newUserData);
这里提到了 getJson 方法:https://laravel.com/docs/8.x/http-tests 但是在这个页面上,我在测试文件头中看到: 命名空间 Tests\Feature;
但我生成的流明测试文件中没有。
在我的 routes/web.php 我有 :
$router->group(['prefix'=>'api/v1'], function() use($router){
$router->post('/register','AuthController@register');
$router->post('/login', 'AuthController@login');
$router->group(['middleware' => 'auth'], function () use ($router) {
$router->get('/profile', 'UserProfileController@index');
怎么了?
谢谢!
因为您可能必须使用 $this->getJson
而不是 $this->get
。
它不是 http://localhost:8000
,因为你正在测试,你不是在字面上访问 URL。它正在模拟那个。
也请分享您的 api.php
或路由文件和控制器(还有正在处理 URL 的中间件)。
查看 Lumen's documentation 我可以看到没有 getJson
,我的错。您必须改用 $this->json('GET'
。