我在控制器中有这行代码,使用 codeigniter 框架编写。
public function search($bookmarkIndex=0)
{
$searchString = trim($this->input->post("searchString"));
$searchType = trim($this->input->post("searchType"));
$search = array(
'searchString'=>$searchString,
'type'=>$searchType);
// DOING SEARCH HERE...
}
如您所见,该方法使用 CodeIgniter 的 $this->input->post。我发现这很难进行单元测试。如果我需要使用 PHPUnit 对其进行测试,我应该如何设置该值?或者有没有办法嘲笑这个?以下是我当前的单元测试方法。
// inside PHPUNIT TEST FOLDER
public function test_search()
{
// I know this is not the way to set "post", below is just my
// expectation if I were able to set it.
$this->CI->input->post("searchString",'a');
$this->CI->input->post("searchType",'contact');
$searchString = $this->CI->input->post("searchString");
echo $searchString; //always false.
$this->CI->search();
$out = output();
// DO ASSERT HERE...
}