excel函数用法:PHPUnit的模拟对象getMock()函数使用方法举例

  由于在我们代码开发中网网接口平凡但是现实资源往往也不足为此在我们测试过程中也出现了困难方面是时间对等方面来自于开发进度和先关准备资源不充分为此phpunit库早已为我们准备好了getMock来跳过或者模拟些接口思路方法:

  以下假设update是掉外网个接口但是这个时候外网接口还没有写好我们如何办才好呢?那就getMock吧!

  helloworld.php

<?php
helloworld
{
 public function helloworld
 {
  echo "hello the world!";
  
 }
 public function update($arra)
 {
   $arra;
 }
 
 public function update_next_do($arra)
 {
  
  ($this->update($arra)'foos')
  {
    foos;
  }{
    false;
  }
  
 }
  
}
?>


  自动生成helloworldTest.php

<?php
require_once 'helloworld.php';
require_once 'PHPUnit/Framework/TestCase.php';
/**
* helloworld test .
*/
helloworldTest extends PHPUnit_Framework_TestCase {
 
 /**
  * @var helloworld
  */
 private $helloworld;
 
 /**
  * Prepares the environment before running a test.
  */
 protected function Up {
  parent::Up ;
  
  // TODO Auto-generated helloworldTest::Up
  
  $this->helloworld = helloworld(/* parameters */);
 
 }
 
 /**
  * Cleans up the environment after running a test.
  */
 protected function tearDown {
  // TODO Auto-generated helloworldTest::tearDown
  
  $this->helloworld = null;
  
  parent::tearDown ;
 }
 
 /**
  * Constructs the test .
  */
 public function __construct {
  // TODO Auto-generated constructor
 }
 
 /**
  * Tests helloworld->helloworld
  */
 public function testHelloworld {
  // TODO Auto-generated helloworldTest->testHelloworld
  //$this->markTestIncomplete ( "helloworld test not implemented" );
  
  //$this->helloworld->helloworld(/* parameters */);
  $this->assertTrue($this->helloworld->update('foo')'foo'); ////假如接口真实存在直接可以来什么返回什么
  $this->assertFalse($this->helloworld->update('foo')'foos');
  //以下开始构造虚拟接口 helloworld  function update('foo') foos;  
  $observer = $this->getMock('helloworld', .gif' />('update'));
  
    // 设定update思路方法期望值为只被以串“something”为参数
    $observer->expects($this->any)
         ->method('update')
         ->with($this->equalTo('foo'))
         ->will($this->Value('foos'));
    // 创建个Subject对象并附上模拟Observer对象
    $this->assertTrue($observer->update('foo')'foos');//虚拟接口变为:输入foo返回foos了
    $result = $observer->update('foo');//虚拟接口变为:输入foo返回foos了
    $this->assertTrue($this->helloworld->update_next_do($result)"foos");//其余真实思路方法去(虚拟接口)以达到预期结果
  
  
 }
  
}


  注意:在里面多次执行了getMock模拟update,我们用了any如果我们用once第 2次这个虚拟肯定会出错once限定了相关匹配如下:

  表 11.1. 匹配器

  匹配器 含义

  PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount any 返回个匹配器当它评估思路方法被执行0或多次时匹配

  PHPUnit_Framework_MockObject_Matcher_InvokedCount never 返回个匹配器当它评估思路方法被从未被执行时匹配

  PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastOnce atLeastOnce 返回个匹配器当它评估思路方法被至少执行次时匹配

  PHPUnit_Framework_MockObject_Matcher_InvokedCount once 返回个匹配器当它评估思路方法恰好被执行次时匹配

  PHPUnit_Framework_MockObject_Matcher_InvokedCount exactly( $count) 返回个匹配器当它评估思路方法正好被执行$count次时匹配

  PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex at( $index) 返回个匹配器当它评估思路方法在特定$index上被时匹配



Tags:  excel函数的用法 vlookup函数的用法 if函数的用法 excel函数用法

延伸阅读

最新评论

发表评论