mercredi 25 octobre 2017

How can I mock a laravel controller method to check if it received a call to a specific method with appropriate parameters

I am using laravel 5.4 and have a route in my api.php file like below:

Route::middleware('auth:api')->post('/message', 'MessagesController@store');

So initially lets just say I wanted to check that when I make a post call to 'api/message' the store() method is suppose to be called.

Below is my test code that I tried:

$mock = \Mockery::mock(MessagesController::class);
$mock->shouldReceive('store')->once();
$this->app->instance(MessagesController::class, $mock);

$response = $this->call('POST','api/message',[
        'receiver_id' => $receiver->id,
        'message' => 'Testing OneSignal - unit test'
    ]);
\Mockery::close();

When I run the test I get the following error in terminal:

1)Tests\Feature\NotificationTest::a_onesignal_notification_is_sent_when_a_new_message_is_sent Mockery\Exception\InvalidCountException: Method store() from Mockery_1_App_Http_Controllers_MessagesController should be called exactly 1 times but called 0 times.

My intention is not really trying to check if store() method is being called, which I know I have other ways to go around. Within the store method I delegate a method to one of the traits which in returns makes a curl call, I would like to test if that method is receiving the intended paramaters.

Thanks in advance for the help.

Aucun commentaire:

Enregistrer un commentaire