edgreenberg's avatar

Creating a request to test a controller

I'd like to call a controller with a request object. I found this recipe on stack overflow http://stackoverflow.com/questions/27293066/setting-post-data-with-a-laravel-request-object for use with Laravel 4.2, but when I tried it on 5.3 it errors out as follows:

        $request = Request::create('tenant/add','post',$r);      // $r contains an array of post parameters
        $result = Route::dispatch($request);

I get a ReflectionException of "Class web does not exist"

What I'd like is to be able to examine the returned $result to assert various things in the test.

All assistance welcome

Ed Greenberg

0 likes
2 replies
primordial's avatar

An example I use where $this->master is an array of params. There are more command available to interrogate the response.

/**
 * Test we can create a new resource
 *
 * @group barcode
 * @covers ::store
    * @test
 */
public function can_create()
{
    $url = route('masters.store', $this->master );
    $url = str_replace('http://:', '', $url);

    $response = $this->post( $url, $this->master )
         ->seeJson()
         ->seeStatusCode( 200 );
}
Bunch-a-devs's avatar

If you want to create a request, you can use the following:

protected function createRequest(
        $method,
        $content,
        $uri = '/test',
        $server = ['CONTENT_TYPE' => 'application/json'],
        $parameters = [],
        $cookies = [],
        $files = []
    ) {
        $request = new \Illuminate\Http\Request;
        return $request->createFromBase(\Symfony\Component\HttpFoundation\Request::create($uri, $method, $parameters, $cookies, $files, $server, $content));
    }
1 like

Please or to participate in this conversation.