简体   繁体   English

在Laravel 5.3中测试API

[英]Testing API in Laravel 5.3

I am new in Laravel and I am a bit confused on how to test my api in 5.3. 我是Laravel的新手,我对如何在5.3中测试我的api感到有点困惑。 I read the docs and I saw this kind of examples but I don't know if I applied the examples correctly. 我阅读了文档,我看到了这样的例子,但我不知道我是否正确应用了这些例子。 Anyway, I'm always getting an ErrorException 无论如何,我总是得到一个ErrorException

Error Exception 错误异常

I have this one in UserTest.php 我在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']);
   }
}

and I tried this one also, still no go. 我也尝试了这个,仍然没有去。

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

here's my route 这是我的路线

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

Is it correct to do it this way? 这样做是否正确? If no, what's the correct way of testing my api? 如果不是,那么测试我的api的正确方法是什么?

Correct way to call the post is 调用帖子的正确方法是

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

or 要么

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

use an echo and exit in the login function and check whether test hitting the controller function. 在登录功能中使用echo并退出并检查测试是否达到控制器功能。 It will be easier if you share a dummy code of your controller function ,because we could n`t figure out what this variable $e. 如果你共享控制器函数的虚拟代码会更容易,因为我们无法弄清楚这个变量是什么。 Which should be in controller. 哪个应该在控制器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM