我正在使用 PHPUnit 测试一个至少有一个参数的 Controller :
public function testSomething($a,$b='')
{
}
我正在尝试以这种方式进行测试:
$params = array('a'=>'foo','b'=>'bar');
$output = $this->request('POST', ['MyController','Something'], $params);
但是我得到:
ArgumentCountError: Too few arguments to function MyController::Something
如何传递这些参数?
注意:我使用了一个将 PHPUnit 集成到 Codeigniter 的库:
"require-dev": {
"kenjis/ci-phpunit-test": "^0.15.0"
}
最佳答案
您可以使用 CodeIgniter 中的 flash_。试试下面的:
$params = array('a'=>'foo','b'=>'bar');
$this->session->set_flashdata('params ', $params);
$output = $this->request('POST', ['MyController','Something']);
Controller :
在 Controller 中,您可以从 flashdata 函数中获取值。
$this->session->flashdata('params');
或
$a='foo';$b='bar';
$output = $this->request('POST', ['MyController','Something',$a,$b]);
关于php - 如何将参数传递给 Controller (测试),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44900547/