gpt4 book ai didi

php - 在 laravel 4 中使用 phpunit 测试 POST

转载 作者:行者123 更新时间:2023-12-04 18:06:45 24 4
gpt4 key购买 nike

我有一个执行 POST 来创建数据的路由,我正在尝试测试是否一切都应该按应有的方式工作。

我有一个 json 字符串,它将包含我想要测试的值,但到目前为止,当我使用 phpunit 运行测试时,测试总是失败:

此外,我知道 json 字符串只是一个字符串,但我也不确定如何使用 json 字符串来测试输入。

我的路线:

Route::post('/flyer', 'flyersController@store');

public function testFlyersCreation()
{
$this->call('POST', 'flyers');

//Create test json string
$json = '{ "name": "Test1", "email": "test@gmail.com", "contact": "11113333" }';

var_dump(json_decode($json));



}

当我运行 phpunit 时,我的错误指向显示“undefined index: name”的调用 POST

最佳答案

鉴于代码示例实际上什么都不做,我不确定我是否正确理解了这个问题,但是如果您询问如何测试请求中需要 json 数据的发布路由,请查看调用() 方法:

https://github.com/laravel/framework/blob/4.2/src/Illuminate/Foundation/Testing/ApplicationTrait.php

原始发布数据应该在 $content 变量中。

我没有安装 Laravel 4 来测试它,但它在 Laravel 5 中对我有用,其中函数的参数顺序略有不同:

public function testCreateUser()
{
$json = '
{
"email" : "horst.fuchs@example.com",
"first_name" : "Horst",
"last_name" : "Fuchs"
}';

$response = $this->call('POST', 'user/create', array(), array(), array(), array(), $json);

$this->assertResponseOk();
}

关于php - 在 laravel 4 中使用 phpunit 测试 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24857763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com