Unit testing 如何在Laravel 5中测试路线,或尝试;“模拟存根”;什么,或者我不知道TDD

Unit testing 如何在Laravel 5中测试路线,或尝试;“模拟存根”;什么,或者我不知道TDD,unit-testing,routes,laravel-5,mockery,Unit Testing,Routes,Laravel 5,Mockery,我从TDD和Laravel开始。具体来说,我从路线开始。我定义了一些,但定义得很糟糕,当我对TDD的“新”概念感到兴奋时,我想为他们编写一些测试 这个想法是测试路由,并且只测试路由,就像我读到的关于TDD的所有内容一样。我知道我可以做一个$this->call->('METHOD','something'),测试响应是正常的,但我想知道调用了正确控制器的正确方法 所以,我想我可以模仿控制器。这是我第一次尝试: public function test_this_route_work_as_exp

我从TDD和Laravel开始。具体来说,我从路线开始。我定义了一些,但定义得很糟糕,当我对TDD的“新”概念感到兴奋时,我想为他们编写一些测试

这个想法是测试路由,并且只测试路由,就像我读到的关于TDD的所有内容一样。我知道我可以做一个
$this->call->('METHOD','something')
,测试响应是正常的,但我想知道调用了正确控制器的正确方法

所以,我想我可以模仿控制器。这是我第一次尝试:

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'),我知道没关系。但不调用方法show。在响应中可以看到:“方法Mockery\u 0\u App\u Http\u Controllers\u 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) 是否可以“模拟存根”控制器?最好的方法是什么


非常感谢。

我终于拿到了。您需要一个部分模拟。可以这样简单地完成(技巧是包括一个“数组”的方法来mock to mockry::mock):


而且,如果您在
setup()
方法中创建了所有控制器的部分模拟,那么所有路由测试都可以分组在一个(或两个)测试用例中!
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');
}