在laravel 4中使用phpunit测试POST [英] testing a POST using phpunit in laravel 4

查看:107
本文介绍了在laravel 4中使用phpunit测试POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a route that does a POST to create data and I'm trying to test if everything should be working the way it should be.

我有一个json字符串,该字符串将具有我要测试的值,但是到目前为止,当我使用phpunit运行测试时,测试始终会失败:

I have a json string that will have the values that i want to test but so far the test is always failing when I run the test using phpunit:

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

Also,I know the json string is just a string but I'm also unsure of how to use the json string to test for input.

我的路线:

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时,我的错误指向显示未定义索引:名称"的呼叫POST

When i run phpunit, my error points to the call POST that says "undefined index: name"

推荐答案

鉴于代码示例实际上什么也没做,但是如果您要问如何测试发布路线,我不确定我是否正确理解了这个问题在请求中需要json数据,请看一下call()方法:

I'm not sure if i understand the question correctly, given the code example that actually does nothing, but if you're asking how to test a post route which requires json data in the request, take a look at the call() method:

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

原始帖子数据应位于$ content变量中.

raw post data should be in the $content variable.

我没有安装Laravel 4对其进行测试,但是它在Laravel 5中对我有用,该函数的参数顺序稍有不同:

I don't have Laravel 4 installed to test it, but it works for me in Laravel 5, where the function has just slightly different order of params:

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();
}

这篇关于在laravel 4中使用phpunit测试POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆