【问题标题】:Testing API in Laravel 5.3在 Laravel 5.3 中测试 API
【发布时间】:2017-02-15 11:07:49
【问题描述】:

我是 Laravel 的新手,我对如何在 5.3 中测试我的 api 有点困惑。我阅读了文档并看到了这种示例,但我不知道我是否正确应用了这些示例。无论如何,我总是得到一个 ErrorException

Error Exception

我在 UserTest.php 中有这个

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class UserTest extends TestCase
{
   /**
    * A basic test example.
    *
    * @return void
   */
   public function testLoginSuccess()
   {
       $this->post('http://127.0.0.1/identificare_api/public/api/user/login', ['email' => 'identificare@gmail.com', 'password' => 'identificare']);
   }
}

我也试过这个,还是不行。

$this->json('POST', 'user/login', ['email' => 'identificare@gmail.com', 'password' => 'identificare']);

这是我的路线

Route::post('user/login', 'UserController@login');

这样做是否正确?如果不是,测试我的 api 的正确方法是什么?

【问题讨论】:

  • 我猜你的错误处理程序有错误! 'e' 可能是 try-catch 块中缺少的参数?我不知道 app/Exceptions 路径是什么——这在 Laravel 中并不常见,所以如果这是您的自定义类路径,我建议您查看错误所在的第 47 行。 $e 是否像 catch($e Exception) 一样被声明? ...然后就可以调试异常了。

标签: php unit-testing laravel-5.3


【解决方案1】:

发帖的正确称呼是

$this->post('/user/login', array('email' => 'identificare@gmail.com', 'password' => 'identificare'));

$this->call('POST','/user/login', array('email' => 'identificare@gmail.com', 'password' => 'identificare'));

在登录功能中使用回显并退出,并检查测试是否命中控制器功能。 如果您共享控制器功能的虚拟代码会更容易,因为我们无法弄清楚这个变量 $e 是什么。哪个应该在控制器中。

【讨论】:

    猜你喜欢
    • 2017-01-26
    • 2018-01-26
    • 2017-01-18
    • 2017-06-06
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    相关资源
    最近更新 更多