【问题标题】:No Adapter for Mapper Unit Test映射器单元测试没有适配器
【发布时间】:2014-05-02 16:04:48
【问题描述】:

我有一个通过 DI 使用 \Zend\Db\TableGateway\TableGateway 的映射器。我已经为单元测试模拟了它。这是测试:

class EMSMapperTest extends PHPUnit_Framework_TestCase
{

    public function testFetchAllReturnsAllScheduledBlocks()
    {
        $resultSet = new ResultSet();

        $mockTableGateway = $this->getMock(
            'Zend\Db\TableGateway\TableGateway',
            array('select','getTable'),
            array(),
            '',
            false
        );

        $mockTableGateway->expects($this->once())
                         ->method('selectWith')
                         ->with()
                         ->will($this->returnValue($resultSet));

        $mockTableGateway->expects($this->once())
                         ->method('getTable')
                         ->with()
                         ->will($this->returnValue('table'));

        $emsMapper = new EMSMapper($mockTableGateway);

        $this->assertSame($resultSet, $emsMapper->fetchAll());
    }
}

和正在测试的映射器:

class EMSMapper extends BaseMapper
{    
    public function fetchAll( $building = null, 
            $room = null, DateRange $range = null )
    {
        $select = new Select;
        $table = $this->tableGateway->getTable();

        $select->from($table);

        if(!empty($building))
        {
            $select->where(array('buildingCode'=>$building));
        }

        if(!empty($room))
        {
            $select->where(array("room"=>$room));
        }

        if(is_array($range))
        {
            if(!empty($range['start']))
            {
                $select->where("start >= '{$range['start']}'");
            }

            if(!empty($range['stop']))
            {
                $select->where("stop <= '{$range['stop']}'");
            }
        }

        $resultSet = $this->tableGateway->selectWith($select);

        $results = array();

        foreach($resultSet as $r)
        {
            $results[] = $r;
        }

        return $results;
    }
}

在从TableGatewaygetTable() 方法返回一个字符串后,单元测试会显示:

There was 1 error:

1) EMSTest\Model\EMSMapperTest::testFetchAllReturnsAllScheduledBlocks
Zend\Db\TableGateway\Exception\RuntimeException: 
This table does not have an Adapter setup

如果Select 似乎要求提供给from() 方法的表字符串具有与之关联的适配器。如何提供所需适配器的模拟?

感谢您的帮助!

【问题讨论】:

    标签: zend-framework2 phpunit


    【解决方案1】:

    您的代码使用selectWith 的实际代码。这会调用一个引发错误的initialize 方法。

    将你的模拟代码更改为:

    $mockTableGateway = $this->getMock(
            'Zend\Db\TableGateway\TableGateway',
            array('selectWith','getTable'),
            array(),
            '',
            false
        );
    

    这应该正确配置您的模拟。

    http://phpunit.de/manual/current/en/test-doubles.html

    来自手册:

    当提供第二个(可选)参数时,只有名称在数组中的方法才会替换为可配置的测试替身。其他方法的行为没有改变。提供 NULL 作为参数意味着不会替换任何方法。

    所以你在正确的方法上设置了期望,但是用你的模拟替换了错误的方法,所以真正的代码正在执行。

    【讨论】:

    • 你是对的!我还有另一个问题让我挂了,但我找到了。这是我的一个疏忽。感谢您指出。我刚刚开始为我工作中的所有项目编写单元测试。
    猜你喜欢
    • 2022-08-09
    • 1970-01-01
    • 2018-03-09
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    相关资源
    最近更新 更多