Facebook 新开发的PHP Mock工具:FBMock

为了让测试运行得更快更可靠,写测试更方便,人们通常使用Mock,而不是直接使用真正的外部依赖

最近,Facebook写了一个新的PHP Mock工具,用它写的Mock看上去非常干净整洁。

当使用PHP其它的Mock框架时,写的代码需要更多的语句,而且令测试过于依赖具体的实现细节。

比如,当我们用PHPUnit做Mock,只是简单地返回一些值时,代码可能象下面这样

$user = $this->getMock('User')
 ->expects($this->any())
 ->method('getID')
 ->will($this->returnValue(1234);


而使用FBMock时,代码如下:


$user = mock('User')->mockReturn('getID', 1234);

其实,FBMock并不是真正意义上的Mock框架,因为它没有上面代码那样,使用期望值检查。所以,只能算是一个有spy功能的桩。其实,Mock框架中的这种期望值检查最好少用,因为它有点儿过于限定化了。上面用FBMock的例子中,其实并不在意getID()被调用了多少次。


当然,有的时候检查哪个method被调用过也是很重要的,这时候可以简单地依赖于PHPUnit中的assertion。


$logger = mock('Logger');

// Run code that uses $logger

// Make sure 'data' was logged
$this->assertCalledOnce($logger, 'log', array('data'));

目前,FBMock支持 Zend PHP 5.4+ 和 HipHop VM
 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shmock(SHorthand for MOCKing)是 PHPUnit 创建 mocks 的平稳替代,使用 EasyMockmock/replay 概念,但是又使用 mocking 定义的闭包范围。 示例代码: <?php     namespace Foo;     /**     * Here's a class we're trying to test yay.     */     class Foo     {         private $foo = 0;         private $incrementing_service = null;         public function __construct(Incrementing_Service $incrementing_service)         {             $this->incrementing_service = $incrementing_service;         }         public function next_foo()         {             $this->foo = $this->incrementing_service->increment($this->foo);             return $this->foo;         }     }     /**     * Our test case runs the same test case twice - once with the original PHPUnit mocking     * syntax and a second time with Shmock syntax.     */     class Foo_Test extends PHPUnit_Framework_TestCase     {                 use \Shmock\Shmockers; // This enables the use of the Shmock helper methods (replicated below)                 public function test_phpunit_original_mocking_syntax()         {             // this is the original PHPUnit mock syntax             $incrementing_service_mock = $this->getMock('\Foo\Incrementing_Service', array('increment'));             $incrementing_service_mock->expects($this->once())                 ->method('increment')                 ->with($this->equalTo(0))                 ->will($this->returnValue(1));             $foo = new Foo($incrementing_service_mock);             $this->assertEquals(1, $foo->next_foo(0));         }         /**         * Create a shmock representation for $class_name and configure expected         * mock interaction with $conf_closure         * @return Shmock A fully configured mock object         * @note You do not need this protected method if you use the Shmockers trait, shown above         */         protected function shmock($class_name, $conf_closure)         {             return \Shmock\Shmock::create_class($this, $class_name, $conf_closure);         }         public function test_shmock_syntax()         {             // here's shmock. Neat huh?             $incrementing_service_mock = $this->shmock('\Foo\Incrementing_Service', function($shmock)             {                 $shmock->increment(0)->return_value(1);             });             $foo = new Foo($incrementing_service_mock);             $this->assertEquals(1, $foo->next_foo(0));         }     } 标签:Shmock

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值