简体   繁体   English

如何在单元测试中将 object 作为参数传递? (拉拉维尔)

[英]How to pass an object as parameter in unit test? (laravel)

I have a data object below that I tried to test using postman.我有一个数据 object 下面我尝试使用 postman 进行测试。 I just don't know how to make this possible also in laravel's unit testing.我只是不知道如何在 laravel 的单元测试中实现这一点。 I have visited this link but it doesn't helped in my case.我已经访问过这个链接,但对我来说没有帮助。

{
    "user_id": 1,
    "name": "juan",
    "address": [
        "state": "Auckland",
        "country": "New Zealand",
    ],
    "school": [
        {
            "school_name": "Kristin School",
            "year": 2016
        },
        {
            "school_name": "ACG Parnell College",
            "year": 2018
        },
    ],
    // and more...
}

How can we pass such an object above in unit testing?我们如何在单元测试中通过上面这样的object? Is there any way to achieve this?有什么办法可以做到这一点?

$response = $this->post('/logbooks/user', ['Authorization': 'Bearer' + $token]);

As you can find in the Laravel docs for HTTP tests , you can send a php array as follows:正如您在HTTP 测试的 Laravel 文档中找到的那样,您可以发送一个 php 数组,如下所示:

$payload = [
    "user_id" => 1,
    "name" => "juan",
    "address" => [
        "state" => "Auckland",
        "country" => "New Zealand",
    ],
];


$response = $this->postJson(
    '/logbooks/user', $payload, ['Authorization': 'Bearer' + $token]
);

It takes your php $payload array and encodes it as JSON and sends it.它获取您的 php $payload数组并将其编码为 JSON 并发送它。 If you have an object you can cast it with $myArray = (array) $myObject .如果你有一个 object 你可以用$myArray = (array) $myObject来投射它。

First to know that such tests are not a "unit test" type.首先要知道这样的测试不是“单元测试”类型。 Note that the code will run and you should be alert to changes that running the code may entail (updating data in the database, contacting external providers).请注意,代码将运行,您应该注意运行代码可能需要的更改(更新数据库中的数据,联系外部提供商)。

Of course, all of this can be mocked, and it is recommended that you read about it carefully.当然,这一切都是可以嘲笑的,建议大家仔细阅读。

class FooTest extends TestCase {
 public function testBar()
 {
   $response = $this->post('/api/v1/yourapi', 
           [
            "user_id" => 1, 
            "name" => "juan", 
            "address" => [
            "state" => "Auckland", 
            "country" => "New Zealand" 
             ], 
            "school" => [
              [
               "school_name" => "Kristin School", 
               "year" => 2016 
            ], 
            [
                  "school_name" => "ACG Parnell College", 
                  "year" => 2018 
               ] 
         ] 
]);
    $response->assertStatus(200);
    $response->assertJsonStructure([...]);

    $response->assert....();
 }
}

As I wrote in the comment below, the JSON you attached was invalid.正如我在下面的评论中所写,您附加的 JSON 无效。 You treated the array as an object, adding unnecessary commas.您将数组视为 object,添加了不必要的逗号。

It is recommended to use an array that the IDE knows to recognize推荐使用IDE知道识别的阵列

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

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