Apr 4, 2023
0
Level 12
Spy on the paginate method, inside a controller action
Hello everyone, how can I test the value that is being passed to the paginate method? Am I missing something? Is there another approach?
Code
Test
public function it_paginates_by_10(): void
{
$spy = $this->spy(BusinessCard::class)->makePartial();
$this->get(route('api.business-cards.index'));
$spy->shouldHaveReceived('paginate')->once()->with(10);
}
Controller
public function index()
{
return BusinessCardResource::collection(BusinessCard::paginate(BusinessCard::PER_PAGE));
}
Test Result
1) Tests\Feature\Api\ListBusinessCardsTest::it_paginates_by_10
Mockery\Exception\InvalidCountException: Method paginate(<Any Arguments>) from Mockery_2_App_Models_BusinessCard should be called
at least 1 times but called 0 times.
/Users/arthur/Dev/Php/business-card/vendor/mockery/mockery/library/Mockery/CountValidator/AtLeast.php:47
/Users/arthur/Dev/Php/business-card/vendor/mockery/mockery/library/Mockery/Expectation.php:308
/Users/arthur/Dev/Php/business-card/vendor/mockery/mockery/library/Mockery/ReceivedMethodCalls.php:46
/Users/arthur/Dev/Php/business-card/vendor/mockery/mockery/library/Mockery/VerificationDirector.php:36
/Users/arthur/Dev/Php/business-card/tests/Feature/Api/ListBusinessCardsTest.php:56
/Users/arthur/Dev/Php/business-card/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:173
Please or to participate in this conversation.