PHP Laravel tests using mocking
<p>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:</p>

<p>My repository code:</p>

<pre><code>   public function page(PaginationQuery $pagination)
    {
        $paginator = new Paginator($this-&gt;model);
        return $paginator-&gt;paginate($pagination-&gt;limit(), $pagination-&gt;direction(), $pagination-&gt;cursor());
    }
</code></pre>

<p>My test:</p>

<pre><code>public function test_paging_with_mocking()
{
    // seed articles
    $seededArticles = [
        new Article(['title' =&gt; 'Article 1']),
        new Article(['title' =&gt; 'Article 2']),
        new Article(['title' =&gt; 'Article 3']),
        new Article(['title' =&gt; 'Article 4']),
        new Article(['title' =&gt; 'Article 5']),
        new Article(['title' =&gt; 'Article 6']),
    ];

    foreach ($seededArticles as $seededArticle) {
        $seededArticle-&gt;save();
    }

    $limit =  PaginationQuery::defaultLimit();
    $direction = PaginationQuery::defaultDirection();
    $cursor = PaginationQuery::defaultCursor();

    $pagination = new PaginationQuery($limit, $direction, $cursor);

    $mPaginator = M::mock(Paginator::class);
    $mPaginator-&gt;shouldReceive('paginate')-&gt;with($limit, $direction, $cursor)-&gt;andReturn([]);

    $receivedResult = $this-&gt;repo-&gt;page($pagination);
}
</code></pre>

<p>I need to achieve a couple of things here:</p>

<ul>
<li><p>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.</p></li>
<li><p>I also need to inject the paginator class in page() method since mocking a <code>new</code> instance of a class will cause an error in mocking this class</p></li>
<li><p>in this line of the test <code>$mPaginator-&gt;shouldReceive('paginate')-&gt;with($limit, $direction, $cursor)-&gt;andReturn([]);</code> I need to add once() but when doing so I am getting the following error: </p>

<p>should be called
 exactly 1 times but called 0 times</p></li>
</ul>