How to test routes in Laravel 5 or try to “MockStub” something, or I don’t know TDD

I start with TDD and Laravel. In particular, I start with routes. I defined some, and I defined it poorly, so I was delighted with the "new" concept of TDD, I wanted to write some test for them.

The idea was to check routes, and only routes, in isolation, since everything I read about TDD recommendations. I know what I can do $this->call->('METHOD','something'), and the test answer is normal or something else, but I would like to know that the right method of the right controller is being called.

So, I thought I could mock the controller. This was my first attempt:

public function test_this_route_work_as_expected_mocking_the_controller()
{
    //Create the mock
    $drawController = \Mockery::mock('App\Http\Controllers\DrawController');
    $drawController->shouldReceive('show')->once();

    // Bind instance of my controller to the mock
    App::instance('App\Http\Controllers\DrawController', $drawController);

    $response = $this->call('GET','/draw/1');

    // To see what fails. .env debugging is on
    print($response);
}

Route::resource('draw', 'DrawController');, , . . : " Mockery_0_App_Http_Controllers_DrawController:: getAfterFilters() ". :

$drawController->getAfterFilters()->willReturn(array());

:

BadMethodCallException: Method Mockery_0_App_Http_Controllers_DrawController::getAfterFilters() does not exist on this mock object

:

public function test_this_route_work_as_expected_mocking_the_controller_workaround()
{
    //Create the mock
    $drawController = \Mockery::mock('App\Http\Controllers\DrawController');
    // These are the methods I would like to 'stub' in this mock
    $drawController->shouldReceive('getAfterFilters')->atMost(1000)->andReturn(array());
    $drawController->shouldReceive('getBeforeFilters')->atMost(1000)->andReturn(array());
    $drawController->shouldReceive('getMiddleware')->atMost(1000)->andReturn(array());
    // This is where the corresponding method is called. I can assume all is OK if we arrive here with
    // the right method name:
    // public function callAction($method, $parameters)
    $drawController->shouldReceive('callAction')->once()->with('show',Mockery::any());

    // Bind instance of my controller to the mock
    App::instance('App\Http\Controllers\DrawController', $drawController);

    //Act
    $response = $this->call('GET','/draw/1');
}

shouldReceives willReturns: atMost(1000) . :

1) Laravel 5? , , , ,

2) "MockStub" ? ?

.

+3
1

, , . . , ( "" mockery:: mock):

public function test_this_route_work_as_expected_mocking_partially_the_controller()
{
    //Create the mock
    $drawController = \Mockery::mock('App\Http\Controllers\DrawController[show]');
    $drawController->shouldReceive('show')->once();

    // Bind instance of my controller to the mock
    App::instance('App\Http\Controllers\DrawController', $drawController);

    //Act
    $this->call('GET','/draw/1');
}

setup(), ( ) TestCases

+8

All Articles