Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 嘲弄MongoCollection时的奇怪行为_Php_Mocking_Phpunit - Fatal编程技术网

Php 嘲弄MongoCollection时的奇怪行为

Php 嘲弄MongoCollection时的奇怪行为,php,mocking,phpunit,Php,Mocking,Phpunit,假设我有这个测试: public function testStorage() { $collection = $this->getMockBuilder('MongoCollection')->disableOriginalConstructor()->getMock(); $collection->method('findOne')->will($this->returnValueMap([ [['_id' => '

假设我有这个测试:

public function testStorage()
{
    $collection = $this->getMockBuilder('MongoCollection')->disableOriginalConstructor()->getMock();

    $collection->method('findOne')->will($this->returnValueMap([
        [['_id' => 'aaa'], ['content'], 'ccc'],
    ]));

    $this->assertEquals('ccc', $collection->findOne(['_id' => 'aaa'], ['content']));
}
当运行单元测试时,它说:断言null与预期的“ccc”匹配失败。 我不明白为什么。但如果我切换到mock另一个函数,比如:find它应该工作

public function testStorage()
{
    $collection = $this->getMockBuilder('MongoCollection')->disableOriginalConstructor()->getMock();

    $collection->method('find')->will($this->returnValueMap([
        [['_id' => 'aaa'], ['content'], 'ccc'],
    ]));

    $this->assertEquals('ccc', $collection->find(['_id' => 'aaa'], ['content']));
}
OK(1个测试,1个断言)


我真的很感谢你的帮助!多谢各位

我使用getMock,它对我很有用:

$collection = $this->getMock('MongoCollection', ['find', 'findOne'], [], '', false);
        $collection->method('find')->will($this->returnValue([
                [['_id' => 'aaa'], ['content'], 'ccc']
            ]));
        $collection->method('findOne')->will($this->returnValue([
            [['_id' => 'aaa'], ['content'], 'ccc']
        ]));