brlebtag's avatar

How to Pass arguments to function in test

I have the following function to test:

public function save(ServiceRequest $request, 
        User $usuario, 
        Service $service = null);

In my routes.php I have

Route::post('service/create', 'ServiceController@save');

In my RouteServiceProvider I have:

$router->model('service', 'App\Service');

There are no service key in Route::post('service/create', 'ServiceController@save'); ( I use service key in other route). But then I check in ServiceController $service and it is not equals to null but in fact, there is a service elloquent model...

how do I pass arguments to avoid it? I tried

$this->actingAs($user);
$this->post('service/create', ['_token' => csrf_token(), 'service' => null])
    ->withSession(['status' => 'The execution was a success!']) 
    ->seePageIs('service/create')
    ;

But it didn't work.

0 likes
7 replies
pmall's avatar

Isn't it this->app->instance(...) ??

brlebtag's avatar

@pmall it was my fault. I was checking the wrong function and in the function wasn't mocking the object. Well I changed my question now. sorry for the trouble...

pmall's avatar

Sorry but I don't understand the question, try to be clearer.

I suggest to get away from route model binding, it leads to messiness like this.

brlebtag's avatar

@pmall sorry again. I am a bit in a hurry so I am making a lot of mistakes in the middle of the way.

Well, My problem is:

I have a model binding. But the route I am trying to test there is no key to search in the database so I thought $service would be null. But it's not.

I tried this:

$this->actingAs($user);
$this->post('service/create', ['_token' => csrf_token(), 'service' => null])
    ->withSession(['status' => 'The execution was a success!']) 
    ->seePageIs('service/create')
    ;

but it didn't work...

pmall's avatar

What do you call a key and what is in $service ?

Your route url pattern doesn't have a service parameter so there is no way the binding gets resolved. Why do you send 'service' => null when you call the route ?

brlebtag's avatar

I am calling key a Route parameters.

$service is:

public function save(ServiceRequest $request, 
        User $user, 
        Service $service = null); //<-- here

I am trying to use save() function to save a new service or to save modification in a existing one. I have the following routes:

Route::post('service/create', 'ServiceController@save');
Route::post('service/{service}/edit', 'ServiceController@save')
    ->where('service', '[0-9]+');

and I have the model binding:

$router->model('service', 'App\Service'); //App\Service is a eloquent model

I thought: When I visit service/create, $service in save() would be null, and when I visit service/{service}/edit, $service would be injected with the corresponding eloquent model. I thought that maybe I should call like this:

$this->actingAs($user);
$this->post('service/create', ['_token' => csrf_token(), 'service' => null])
    ->withSession(['status' => 'The execution was a success!']) 
    ->seePageIs('service/create')
    ;

to make $service be equals to null.

pmall's avatar
pmall
Best Answer
Level 56

The laravel way of doing things is to have separate method for store and update. You should keep this convention.

Then you should follow the rest convention too, it simplify things :

  • Show the create form for a model (ex : an article) : GET /articles/create => ArticlesController@create
  • Store a model : POST /articles => ArticlesController@store
  • Edit a model : GET /articles/{article_id}/edit => ArticlesController@edit
  • Update a model : PUT /articles/{article_id} => ArticlesController@update

And I think you should not pass any parameter named service when calling the route, it default to null if not present anyway.

1 like

Please or to participate in this conversation.