Home > Backend Development > PHP Tutorial > Facebook's newly developed PHP Mock tool: FBMock_PHP tutorial

Facebook's newly developed PHP Mock tool: FBMock_PHP tutorial

WBOY
Release: 2016-07-14 10:09:12
Original
1432 people have browsed it

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.

www.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,...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template