dongqiuge5435 2012-10-03 09:17
浏览 84
已采纳

在PHPUnit Mock对象中返回不同的结果

I've been working on getting our systems more compatible with PHPUnit so we can do more unit testing of our classes and have managed to get some of them working with mock objects, but I've run across a problem which I can't seem to get around.

One of the classes we have (which I'm creating a mock version of) is for queries. You pass the query string into it's "query()" method, it logs the query, runs it and returns the result. It also wraps the mysql_fetch_assoc with a method called "get_row()", which returns an array value much like the original.

The problem is that, in some methods, there's more than one query being passed to the "query()" method and as a result it needs to run through multiple while loops to load the data into different variables. I've created a simplified version below:

class object{
    public function __construct($query){
        $this->query = $query;
    }

    public function loadData(){

        $data1 = queryDataSource("SELECT * FROM data1");
        $data2 = queryDataSource("SELECT * FROM data2");

        return Array(
            "data1" => $data1,
            "data2" => $data2,
        );

    }

    private function queryDataSource($query){
        $this->query->query($query)

        while($row = $this->query->get_row()){
            $result[] = $row;
        }

        return $result
    }
}

class testObject extends PHPUnit_Framework_TestCase{
    method testLoadData(){
        $test_data = Array('name' => 'Bob', 'number' => '98210');

        $query = $this->getMock('Query');
        $query->expects($this->any())->method('query');
        $query->expects($this->at(1))->method('get_row')->will($this->returnValue($test_data);
        $query->expects($this->at(2))->method('get_row')->will($this->returnValue(False);
        $query->expects($this->at(3))->method('get_row')->will($this->returnValue($test_data);
        $query->expects($this->at(4))->method('get_row')->will($this->returnValue(False);
    }
}

In order to escape the first while loop in $object->queryDataSource() I'm returning a boolean FALSE value, as would happen when doing mysql_fetch_assoc. The problem is that, when it tries to run the second query and fetch the data through get_row(), the mock object seems to keep returning FALSE ratehr than moving on to the at(3) point. This happens even with 4 objects, only the first will get the test data as a return value then get FALSE the second time, the others will get FALSE every time.

Does anyone know if there's a way to get around this? I tried removing the FALSE flags and just having the odd values in at(), but that had the same problem, and I tried just having it return the data for at(1-2), but that just passed all the data into the first while loop and nothing for the other.

Thanks for any help you can give, hope the description of the problem's clear enough

  • 写回答

2条回答 默认 最新

  • dongyuqie4322 2012-10-03 14:30
    关注

    I can't run the code as it seems to only be pseudocode but from what I understood is that you are trying to mock like this:

    Call to query, get_row, get_row, query, get_row, get_row.
    

    The issue you seem to have run into is that the number in the ->at() matcher doesn't count up per method but per object.

    So what you probably want to write is:

        $query->expects($this->any())->method('query');
        $query->expects($this->at(1))->method('get_row')->will($this->returnValue($test_data);
        $query->expects($this->at(2))->method('get_row')->will($this->returnValue(False);
        $query->expects($this->at(4))->method('get_row')->will($this->returnValue($test_data);
        $query->expects($this->at(5))->method('get_row')->will($this->returnValue(False);
    

    Or to make it a litte easer to read maybe even:

        $query->expects($this->at(0))->method('query');
        $query->expects($this->at(1))->method('get_row')->will($this->returnValue($test_data);
        $query->expects($this->at(2))->method('get_row')->will($this->returnValue(False);
        $query->expects($this->at(3))->method('query');
        $query->expects($this->at(4))->method('get_row')->will($this->returnValue($test_data);
        $query->expects($this->at(5))->method('get_row')->will($this->returnValue(False);
    

    With your mocks you ran into the issue that the second call to "query" was counting up one "call" and hence skipping over the second return($test_data);.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器