为什么PHPUnit寄存器测试返回405



我想为流明应用程序做phpunit测试,比如:

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方法不允许?我是邮递员,我检查我的方法:https://prnt.sc/207bu03

poster中的方法/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但在这个页面上,我在测试文件标题中看到:命名空间测试\功能;

但在我生成的流明测试文件中没有。

在我的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的文档,我可以看到没有getJson,这是我的错。您必须使用$this->json('GET'

最新更新