控制器中的laravel单元测试索引功能

this is my index function

public function index($alias, $profileId)
{
    $this->setClientAndClientProfile($alias, $profileId);

    $routeData = Routedata
                        ::where('client_id', $this->client->id)
                        ->paginate(10);

    return view('client.route_data.index', compact('routedata'))->with('client', $this->client)->with('clientProfile', $this->clientProfile);
}

setClientAndClientProfile is function just to check type of user and bring his profile

so how to write a test for this function?

In your case, you can simply assert that the view returned by the route has routedata.

public function testIndex()
{
    $this->call('GET', '/path/to/my/controlller/method');

    $this->assertViewHas('routedata');
    $this->assertViewHas('client');
    $this->assertViewHas('clientProfile');
}

However, you could take this one step further and you could make assertions about the type of data that was sent to the view.

First, grab the data:

$routedata = $response->original->getData()['routedata'];
$client = $response->original->getData()['client'];
$clientProfile = $response->original->getData()['clientProfile '];

Now you can test the instances of these variables to ensure they were properly set as well:

$this->assertInstanceOf('\Illuminate\Database\Eloquent\Collection', $routedata);
$this->assertInstanceOf('\App\Client', $client);
$this->assertInstanceOf('\App\ClientProfile', $clientProfile);

All together it would be something like:

public function testIndex()
{
    $this->call('GET', '/path/to/my/controlller/method');

    $this->assertViewHas('routedata');
    $this->assertViewHas('client');
    $this->assertViewHas('clientProfile');

    $routedata = $response->original->getData()['routedata'];
    $client = $response->original->getData()['client'];
    $clientProfile = $response->original->getData()['clientProfile '];

    $this->assertInstanceOf('\Illuminate\Database\Eloquent\Collection', $routedata);
    $this->assertInstanceOf('\App\Client', $client);
    $this->assertInstanceOf('\App\ClientProfile', $clientProfile);

}

I made assumptions about the types of $client and $clientProfile, so you should adjust the classes accordingly.

Hopefully this helps.