php - 如何将参数传递给 Controller ​​(测试)

标签 php codeigniter testing phpunit

我正在使用 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/

相关文章:

javascript - 正则表达式用不同条件替换字符串中的前零两次

node.js - 为什么带有nodeunit的nodejs测试用例在成功通过测试用例后仍然在webstorm中加载?

ruby-on-rails - rails : How do I write tests for a ruby module?

ruby - 为什么使用 Guard 时我的规范这么慢?

PHP/mySQLi 不查询

php - 无法删除任何元组

php - 将图像插入数据库

codeigniter - 代码点火器模型到模型关系

php - 子div不随父调整大小

mysql - 如果表为空,则 CodeIgniter result_array() 出现 bool 错误