【问题标题】:Unit Testing with CodeIgniter's post使用 CodeIgniter 的帖子进行单元测试
【发布时间】:2023-03-29 22:20:01
【问题描述】:

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

【问题讨论】:

    标签: php codeigniter phpunit


    【解决方案1】:

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

    public function test_search()
    {
    
        $_POST["searchString"] = 'a';
        $_POST["searchType"] = 'contact';
    
        $this->CI->search();
        $out = output();
    
        $searchString = $this->CI->input->post("searchString");
        echo $searchString;
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多