gpt4 book ai didi

unit-testing - 如何在 Laravel 5 中测试路由,或者尝试 "MockStub"的东西,或者我不知道 TDD

转载 作者:行者123 更新时间:2023-12-01 08:54:49 30 4
gpt4 key购买 nike

我从 TDD 和 Laravel 开始。具体来说,我从路线开始。我定义了一些,但定义得很糟糕,因为我对 TDD 的"new"概念感到非常兴奋,我想为它们编写一些测试。

这个想法是测试路由,并且只测试路由,隔离,因为我读过的关于 TDD 的所有内容都是推荐的。我知道我可以做一个 $this->call->('METHOD','something') 并且测试响应正常或其他什么,但我想知道调用了正确 Controller 的正确方法。

所以,我认为我可以模拟 Controller 。这是我的第一次尝试:

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_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');
}

但是我想更改 shouldReceiveswillReturns : atMost(1000) 正在伤害我的眼睛。所以我的问题是:

1) 有没有更干净的方法来测试 Laravel 5 中的路线?我的意思是,理想的情况是 Controller 不存在,但是,如果路由没问题,测试通过

2)是否可以“MockStub” Controller ?什么是更好的方法?

非常感谢你。

最佳答案

我终于明白了。您需要一个 部分模拟 。它可以像这样简单地完成(诀窍是包括一个“数组”的方法来模拟 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() 方法中创建所有 Controller 的部分模拟,则所有路由测试都可以分组在一个(或几个)TestCases 中

关于unit-testing - 如何在 Laravel 5 中测试路由,或者尝试 "MockStub"的东西,或者我不知道 TDD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29422426/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com