Don Djoe
Don Djoe

Reputation: 705

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

Upvotes: 6

Views: 6708

Answers (2)

muya_
muya_

Reputation: 1028

When using the alternative action() method, make sure you add an extra array after $uri, the signature is a bit different, it has the $wildcards array:

Response::action(
    string $method, 
    string $action, 
    array $wildcards = array(), 
    array $parameters = array(), 
    array $files = array(), 
    array $server = array(), 
    string $content = null, 
    bool $changeHistory = true
)

Upvotes: 1

mwpeng 彭明伟
mwpeng 彭明伟

Reputation: 96

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

Upvotes: 4

Related Questions