1

我在控制器中有这行代码,使用 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... 
}
4

1 回答 1

2

使用 $POST 将单元测试更改为下面,它工作正常。

public function test_search()
{

    $_POST["searchString"] = 'a';
    $_POST["searchType"] = 'contact';

    $this->CI->search();
    $out = output();

    $searchString = $this->CI->input->post("searchString");
    echo $searchString;
}
于 2013-11-13T05:48:20.470 回答