miigaa's avatar
Level 24

Can't mock my repository in acceptance test!

Please see the code below. The getUnconfirmedStudentsCount mocked method doesn't call. My tests are fail.

<?php

use Mockery as m;
use App\Models\Student;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;

class DashboardStatsTest extends TestCase
{
    use DatabaseTransactions;

    /** @test */
    public function an_employee_sees_the_count_of_unconfirmed_students()
    {
        $this->instance(
            'App\Contracts\Repositories\StudentRepository', 
            $studentRepository = m::mock('App\Contracts\Repositories\StudentRepository')
        );

        $studentRepository->shouldReceive('getUnconfirmedStudentsCount')->once()->andReturn(10300);

        $this->visit('/')->see(10300);
    }
}
0 likes
4 replies
miigaa's avatar
Level 24

Problem solved. Sorry it was completely my bad. I forgot to sign in an employee.

davorminchorov's avatar

Wouldn't it be easier if you just write integration tests for your repository classes? That's what I did on a previous project.

1 like
zachleigh's avatar

Yes. What @Ruffles said. Why mock them at all? I just hit mine either directly or through the controller method that calls them.

miigaa's avatar
Level 24

Thanks!

i really agree with you guys. But i've already tested that method at the unit test level. I thought, If i choose to hit the database then i should provide 10300 or more students. It slows down my tests.

Refactored version:

/** @test */
public function an_employee_sees_the_count_of_unconfirmed_students()
{
    $this->mock('App\Contracts\Repositories\StudentRepository')
        ->shouldReceive('countUnconfirmed')
        ->once()
        ->andReturn(10300);

    $this->actingAsEmployee()
        ->visit('/')
        ->see('Unconfirmed Students')
        ->see(10300);
}

Please or to participate in this conversation.