I am writing tests for my repository, and I have a page() method that takes a PaginationQuery object which consists of limit, direction and cursor for a specific model and returns a json paginator object, I am struggling with the mocking, here is my code:
My repository code:
public function page(PaginationQuery $pagination)
{
$paginator = new Paginator($this->model);
return $paginator->paginate($pagination->limit(), $pagination->direction(), $pagination->cursor());
}
My test:
public function test_paging_with_mocking()
{
// seed articles
$seededArticles = [
new Article(['title' => 'Article 1']),
new Article(['title' => 'Article 2']),
new Article(['title' => 'Article 3']),
new Article(['title' => 'Article 4']),
new Article(['title' => 'Article 5']),
new Article(['title' => 'Article 6']),
];
foreach ($seededArticles as $seededArticle) {
$seededArticle->save();
}
$limit = PaginationQuery::defaultLimit();
$direction = PaginationQuery::defaultDirection();
$cursor = PaginationQuery::defaultCursor();
$pagination = new PaginationQuery($limit, $direction, $cursor);
$mPaginator = M::mock(Paginator::class);
$mPaginator->shouldReceive('paginate')->with($limit, $direction, $cursor)->andReturn([]);
$receivedResult = $this->repo->page($pagination);
}
I need to achieve a couple of things here:
-
to test the page() method while mocking the paginator since I don't care here about the output, I only need to assert that the limit, direction and cursor values are being passed correctly to the paginate() method.
-
I also need to inject the paginator class in page() method since mocking a
newinstance of a class will cause an error in mocking this class -
in this line of the test
$mPaginator->shouldReceive('paginate')->with($limit, $direction, $cursor)->andReturn([]);I need to add once() but when doing so I am getting the following error:should be called exactly 1 times but called 0 times
Aucun commentaire:
Enregistrer un commentaire