2

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

9 Comments

without make login test ?
and what do you mean in this '/path/to/my/controller/method' u mean the controller file that contain index method?
@flower The route. So if it was the ClientProfile controller, maybe /client-profiles
i checked from route list i see this | | GET|HEAD | backend/route-data | backend.route-data.index | App\Http\Controllers\Backend\RoutedataController@index | App\Http\Middleware\Backend |
@flower Then you would use /backend-route-data as your route.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.