简体   繁体   English

Laravel 5.1使用JSON对API进行PHPUnit测试

[英]Laravel 5.1 PHPUnit Tests for API using JSON

I just started working with Unit testing in Laravel 5.1 to test an API I'm building, and I can get PHPUnit to work fine with the ExampleTest.php, but when I create my own test, it fails every time. 我刚刚开始在Laravel 5.1中使用单元测试来测试要构建的API,并且可以使PHPUnit与ExampleTest.php一起正常工作,但是当我创建自己的测试时,每次都会失败。

Here is the response for my API endpoint v1/conversations : 这是我的API端点v1/conversations的响应:

{
  "data": [
    {
      "id": 1,
      "created_at": "2015-07-05",
      "updated_at": "2015-07-07"
    },
    {
      "id": 2,
      "created_at": "2015-07-06",
      "updated_at": "2015-07-08"
    }
  ]
}

Here is my ConversationsTest.php: 这是我的ConversationsTest.php:

<?php

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

class ConversationsTest extends TestCase
{

    public function test_list_conversations()
    {
        $this->get('v1/conversations')->seeJson('data');
    }
}

But when I got to run my test, I get the following: 但是当我必须运行测试时,我得到以下信息:

There was 1 error:

1) ConversationsTest::test_list_conversations
ErrorException: Argument 1 passed to Illuminate\Foundation\Testing\TestCase::seeJson() must be of the type array, string given

Isn't my API returning valid JSON data? 我的API是否不返回有效的JSON数据? Why can't Laravel's seeJson method interpret the response? 为什么Laravel的seeJson方法不能解释响应? I've tried to follow the Laravel 5.1 Testing APIs documentation , but I'm clearly missing something... 我试图遵循Laravel 5.1 Testing APIs文档 ,但显然缺少某些内容...

The data returned by your endpoint is fine and should not be modified. 端点返回的数据很好,不应修改。 The error is simply telling you that you are passing a string into the function seeJson('data'); 错误只是告诉您正在将字符串传递到函数seeJson('data');

According to this here, https://github.com/laracasts/Integrated/wiki/Testing-APIs , you should be able to use it like so just to verify that some JSON is being returned... 根据这里https://github.com/laracasts/Integrated/wiki/Testing-APIs的说明 ,您应该能够像这样使用它,以便验证是否已返回某些JSON ...

$this->get('v1/conversations')->seeJson();

You can also pass in an array to this function to see if a certain part of the data exists... 您还可以将数组传递给此函数,以查看数据的特定部分是否存在...

$this->get('v1/conversations')->seeJson([
    'data' => 'some data',
]);

Though that will likely fail unless you pass in the entire array which is also being returned by your endpoint. 尽管除非传入整个数组(端点也将返回该数组),否则这很可能会失败。

The only thing you can not do is pass in a string to this function as it wouldn't know what to do with a string. 您唯一不能做的就是将字符串传递给此函数,因为它不知道如何处理字符串。

Try this 尝试这个

<?php

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

class ConversationsTest extends TestCase
{

    public function test_list_conversations()
    {
        $this->get('v1/conversations')->seeJson(['data']);
    }
}

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

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