New PHP mock tool developed by Facebook: fbmock

Source: Internet
Author: User

In order to make the test run faster and more reliable, it is more convenient to write the test, people usually use mock instead of directly using real external dependencies.

Recently, Facebook wrote a new PHP mock tool that looks clean and tidy.

When using other PHP mock frameworksCodeMore statements are required, and the test is too dependent on the specific implementation details.

For example, when we use phpunit for mock, but simply return some values, the Code may be as follows:

 
$ User = $ this-> getmock ('user')-> expects ($ this-> Any ()-> method ('getid ') -> will ($ this-> returnvalue (1234 );

 
 

When fbmock is used, the Code is as follows:

 
$ User = mock ('user')-> mockreturn ('getid', 1234 );

In fact, fbmock is not a mock framework in the true sense, because it does not use the expected value check as in the above Code. Therefore, it can only be regarded as a pile with the spy function. In fact, it is better to use this Expected Value Check in the mock framework, because it is too restrictive. In the above fbmock example, it does not matter how many times GETID () is called.

Of course, it is also important to check which method has been called. In this case, you can simply rely on assertion in phpunit.

 
$ Logger = mock ('logger '); // run code that uses $ logger // make sure 'data' was logged $ this-> assertcalledonce ($ logger, 'log ', array ('data '));

Currently, fbmock supportsZend PHP 5.4 + andHiphop VM.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.