脚本宝典收集整理的这篇文章主要介绍了php – 支持链接方法的模拟对象,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
$result = $database->select('my_table')->where(array('my_field'=>'a_value'))->limIT(1)->execute();
如果我必须模拟两个不同的选择查询以便它们返回不同的结果,则会出现问题.有任何想法吗?
这是关于PHPUnit的,但其他单元测试框架的经验将有所帮助.
class Stubtest extends PHPUnit_Framework_TestCase { public function testChainingStub() { // Creating the stub with the methods to be called $stub = $this->getMock('Zend_Db_Select',array( 'select','where','limit','execute' ),array(),'',FALSE); // telling the stub to return a certain result on execute $stub->expects($this->any()) ->method('execute') ->will($this->returnValue('exPEcted result')); // telling the stub to return itself on any other calls $stub->expects($this->any()) ->method($this->anything()) ->will($this->returnValue($stub)); // testing that we can chain the stub $this->assertSame( 'expected result',$stub->select('my_table') ->where(array('my_field'=>'a_value')) ->limit(1) ->execute() ); } }
您可以将其与期望结合起来:
class StuBTest extends PHPUnit_Framework_TestCase { public function testChainingStub() { // Creating the stub with the methods to be called $stub = $this->getMock('Zend_Db_Select',FALSE); // overwriting stub to return something when execute is called $stub->expects($this->exactly(1)) ->method('execute') ->will($this->returnValue('expected result')); $stub->expects($this->exactly(1)) ->method('limit') ->with($this->equalTo(1)) ->will($this->returnValue($stub)); $stub->expects($this->exactly(1)) ->method('where') ->with($this->equalTo(array('my_field'=>'a_value'))) ->will($this->returnValue($stub)); $stub->expects($this->exactly(1)) ->method('select') ->with($this->equalTo('my_table')) ->will($this->returnValue($stub)); // testing that we can chain the stub $this->assertSame( 'expected result',$stub->select('my_table') ->where(array('my_field'=>'a_value')) ->limit(1) ->execute() ); } }
以上是脚本宝典为你收集整理的php – 支持链接方法的模拟对象全部内容,希望文章能够帮你解决php – 支持链接方法的模拟对象所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。