lundi 29 janvier 2018

Larvel Mocking interfaces inside controllers

I am trying to write some unit tests for my controllers.

I have implemented an interface and repository, this is passed in to the controller via Dependancy Injection

I have written a unit test that mocks the interface and New's up an instance of a controller.

My test is falling with the following error

Mockery\Exception\InvalidCountException : Method find(<Any Arguments>) from Mockery_0_App_Interfaces_VenueRepositoryInterface should be called
 exactly 1 times but called 2 times.

Ultimately i want to check then when the index method is called on the controller, then it interacts correctly with the interface hence the desire to mock it. I will write further tests to actually test the interface implementation.

VenueControllerTest

<?php

namespace Tests\Unit;


use \Mockery;
use Tests\TestCase;
use App\Http\Controllers\VenueController;

class VenueControllerTest extends TestCase
{

protected $venueRepositoryInterface;

protected $mock;

    public function setUp()
    {
        parent::setUp();

            $this->mock = \Mockery::mock('App\Interfaces\VenueRepositoryInterface');
    }


    public function tearDown()
    {
        Mockery::close();
    }


    public function testControllerIndex()
    {

       $this->mock->shouldReceive('find')->once()->andReturn(2);
       $venueController = new VenueController($this->mock);
       $venueController->index();
       $this->assertEquals($this->mock->find(1),2);
    }

}

VenueController.php

  <?php

namespace App\Http\Controllers;

use App\Interfaces\VenueRepositoryInterface;
use Illuminate\Http\Request;

class VenueController extends Controller
{

protected $venueRepository;

    public function __construct(VenueRepositoryInterface $venueRepository)
    {
        $this->venueRepository = $venueRepository;
    }

    public function index()
    {
        $this->venueRepository->find(1);
        return view( 'venue');
    }

}

VenueRepositoryInterface.php

<?php
// app/Repositories/Contracts/VenueRepositoryInterface.php

namespace App\Interfaces;

interface VenueRepositoryInterface
{
    public function find($id);
    public function findBy($att, $column);
}

?>

VenueRepository.php

<?php
// app/Repositories/venueRepository.php
namespace App\Repositories;

use App\Interfaces\VenueRepositoryInterface;
use App\Venue;

class VenueRepository implements VenueRepositoryInterface
{
    protected $venue;

    public function __construct(Venue $venue)
    {
        $this->venue = $venue;
    }

    public function find($id)
    {
        return 2;
    }

    public function findBy($att, $column)
    {
        return $this->venue->where($att, $column);
    }
}
?>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire