brlebtag's avatar

Route Model Binding problem with Unit testing

I have:

//route.php

Route::pattern('record', '[0-9]+');

post('record/{record}/edit', ['as' => 'record.update', 'uses' =>'Backend\User\RecordController@update']);

//RecordController.php

//..
RecordController::__construct(RecordRepositoryContract $model)
//...
RecordController::update(RecordRequest $request, Record $record)
{
$response = $this->model->update($record, $request->only('name', 'tags'));
//...

I am mocking $request, $model and i am calling 'record.update' in my tests like this:

$response = $this->route('POST', 'record.update', 1);

I have a record id = 1.

but I am getting redirect to '/' and I don't know why...

My RouteServiceProvider.php is like this:

$router->model('record', 'App\Eloquents\Record', function(){
            throw new RecordNotFoundException();
        });

it's not throwing exception just been redirect to '/'. I don't know why...

0 likes
3 replies
brlebtag's avatar

just testing, I added a Route::bind() to Routes.php (other time I test placing it in RouteServiceProvider.php too) like this:

Route::bind('record', function($value)
{
    return \App\Eloquents\Record::whereId($value)->where('user_id', \Auth::id())->firstOrFail();
});

and I monitored any query:

DB::listen(function($sql, $bindings, $time)
{
    echo $sql,"\n";
    var_dump($bindings);
});
//Got this: 
//select * from `fichas` where `fichas`.`id` = ? limit 1
//array(1) {
//  [0] =>
//  string(1) "1"
//}

clearly Route::bind() was not executed and I am being redirected to '/'.

brlebtag's avatar
brlebtag
OP
Best Answer
Level 1

Finally found my problem:

I typed this:

$this->app->instance('App\Contracts\RecordRepositoryContract', $request);

instead of this:

$this->app->instance('App\Http\Requests\RecordRequest', $request);

I guess it's because yesterday was already too late at night, hahaha.

1 like

Please or to participate in this conversation.