PHPUnit模拟MongoCollection时的奇怪行为

时间:2015-12-07 17:19:32

标签: php mocking phpunit

假设我有这个测试:

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'。 我无法弄清楚为什么。但是,如果我切换到模拟另一个函数,让我们说:发现它应该工作。

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']));
}

好(1次测试,1次断言)

非常感谢你的帮助!非常感谢你!

1 个答案:

答案 0 :(得分:0)

我使用getMock并且它为我工作:

//-------------------------------------------------------
Highcharts.Renderer.prototype.symbols.line = function(x, y, width, height) {
    return ['M',x ,y + width / 2,'L',x+height,y + width / 2];
};
//-------------------------------------------------------
Highcharts.setOptions({
    chart:{
        type:'bar',
        margin:[5,15,10,100],
    },
    credits:{enabled:false},
    exporting:{enabled:false},
    legend:{enabled:false},
    title:{text:''},
    xAxis:{
        tickLength:0,
        lineColor:'#999',
        lineWidth:1,
        labels:{style:{fontWeight:'bold'}}
    },
    yAxis:{
        min:0,
        minPadding:0,
        maxPadding:0,
        tickColor:'#ccc',
        tickWidth:1,
        tickLength:3,
        gridLineWidth:0,
        endOnTick:true,
        title:{text: ''},
        labels:{
            y:10,
            style:{
                fontSize:'8px'
            },
            formatter:function(){
                if (this.isLast){
                    return this.value + ' %';
                }
                else{
                    return this.value;
                }
            }
        }
    },
    tooltip:{
        enabled:true,
        backgroundColor:'rgba(255, 255, 255, .85)',
        borderWidth:0,
        shadow:true,
        style:{fontSize:'10px',padding:8},
        formatter:function() {
            return this.series.name + ": <strong>" + Highcharts.numberFormat(this.y,2) + "</strong>";
        }
    },
    plotOptions:{
        bar:{
            color:'#000',
            shadow:false,
            borderWidth:0,
        },
        scatter:{
            marker:{
                symbol:'line',
                lineWidth:3,
                radius:8,
                lineColor:'#000'
            }
        }
    }
});
//-------------------------------------------------------
$(document).ready(function() {

var chart1 = new Highcharts.Chart({
    chart:{renderTo:'container'},
    xAxis:{categories:['bar']},
    yAxis:{
        max:100,
        labels:{y:10,style:{fontSize:'8px'}},
        plotBands:[{from:0,to:70,color: 'rgba(103,103,103,.35)'},
            {from:70,to:85,color: 'rgba(153,153,153,.35)'},
            {from:85,to:100,color: 'rgba(204,204,204,.35)'}]
    },
    series:[{name:'Measure',pointWidth:10,data:[80]},
        {name:'Target',type: 'scatter',data:[90],}]
});
}(jQuery));
//-------------------------------------------------------