Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Unit Test - POST Request Payload

I need help in creating a unit test case for Laravel. It's an API end point accepting a json as the POST payload (NOT POST form) and basically just creating an entry in the database based on that json object. The API works fine but I'm having trouble in creating the unit test.

The controller code contains

$request = Request::instance();
$content = $request->getContent();
$inputRequest = json_decode($content);

I don't know how to pass the payload into the unit test code (extending Illuminate\Foundation\Testing\TestCase)

Anyone able to help?

Thanks heaps

like image 775
Don Djoe Avatar asked Feb 23 '15 03:02

Don Djoe


1 Answers

when using call() method provided by laravel in a unit test.

the sixth parameter $content(String) can be used to pass in the post payload in test case class:

$this->call($method, $uri, $parameters, $files, $server, jsonencode($yourdata), $changeHistory);
like image 99
mwpeng 彭明伟 Avatar answered Sep 25 '22 22:09

mwpeng 彭明伟