douju8782 2019-02-05 14:46
浏览 53
已采纳

无法将$ _GET传递给单元测试

I cannot pass $_GET['month'] and $_GET['day'] to my unit test

$url = "https://api.abalin.net/get/namedays?day=5&month=2";
$response = $this
    ->json('GET', $url);

// tried also this

$var = ['month' => '2', 'day' => '5'];
$url = "https://api.abalin.net/get/namedays?day=5&month=2";

$response = $this
    ->json('GET', $url, $var);

After execution both will fail on this condition

if (!isset($_GET['month']) || !isset($_GET['day'])){
//
}
  • 写回答

1条回答 默认 最新

  • dscbxou1900343 2019-02-06 04:33
    关注

    If you are using laravel, then $_GET is not an ideal way of testing. Also, laravel's testing setup is very nice to avoid doing things by your own.

    This documentation contains examples of what you need.

    Also, unit testing is about testing the expected output or may be testing the process of data transition till the output in some cases.

    In your case if your test itself passes the url parameters in the GET method, then testing if it contains those parameters next to it, is not really helpful. Instead, you should assert tests for the response you expect.

    Here is a very simple example :

    <?php 
    
    $response = $this->json('GET',  '/url',['month' => '2', 'day' => '5']);
    
    // Check if response is successful with status code 200
    $response->assertStatus(200);
    
    // Check if response contains an expected data, for example `status` as `success`
    $response->assertJson(['status' => 'success'];
    

    If you are on laracast, this will get you started.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题