In order to make tests run faster and more reliably, and to write tests more conveniently, people usually use Mocks instead of directly using real external dependencies.
Recently, Facebook has written a new PHP Mock tool, and the Mocks written with it look very clean and tidy.
When using other mocking frameworks for PHP, the code written requires more statements and makes the test too dependent on specific implementation details.
For example, when we use PHPUnit to do Mock and simply return some values, the code may look like the following
[php]
$user = $this->getMock('User') </div>
<div> ->expects($this->any()) </div>
<div> ->method('getID') </div>
<div> ->will($this->returnValue(1234); </div>
<div>
When using FBMock, the code is as follows:
[php]
$user = mock('User')->mockReturn('getID', 1234);
Actually, FBMock is not a Mock framework in the true sense, because it does not use expected value checking like the above code. Therefore, it can only be regarded as a pile with spy function. In fact, this kind of expectation check in the Mock framework is best used sparingly, because it is a bit too restrictive. In the above example using FBMock, it doesn't actually matter how many times getID() is called.
Of course, sometimes it is also important to check which method has been called. In this case, you can simply rely on the assertion in PHPUnit.
[php]
$logger = mock('Logger');
// Run code that uses $logger
// Make sure 'data' was logged
$this->assertCalledOnce($logger, 'log', array('data'));
Currently, FBMock supports Zend PHP 5.4+ and HipHop VM.
http://www.bkjia.com/PHPjc/477686.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477686.htmlTechArticleIn order to make tests run faster and more reliably and write tests more conveniently, people usually use Mock instead of directly Real external dependencies Recently, Facebook wrote a new PHP Mock tool,...