Home > 类库下载 > PHP类库 > First introduction to PHPunit stub simulation return data

First introduction to PHPunit stub simulation return data

高洛峰
Release: 2016-10-09 11:12:16
Original
1267 people have browsed it

This is a combination of PHPunit documentation and the explanations of experts during this period. I want to record the knowledge I have learned, supplement it for future reference, and improve what I have learned

We generally use single testing to test the code in the company's business , he will help find every little place where your thinking is not comprehensive enough. (Although Daniel said that development can write unit tests first and then write implementation code, but now I feel that I am still far away from that)

  Stub (stub):

   Replace the object with ( Optionally) The practical method of returning a test double with a configured return value is called stubbing" - this is the explanation of stubbing given in the official documentation

  I only understand it now when I look back at it, how should I say it? Take a pear.

====Premise

 What I want to test is this method: switchClothes($username) ---- Query the database by name. If the gender is 1, it will return pants, if it is 0, it will return skirts;

<?php
        Class Switch{
            public function switchClothes($username){
                $database=new database();
                $gender=$databse->find("id=$username");
                if($gender==0){
                        return "裙子";
                }else{
                        return "裤子";
                }
            }
        }
Copy after login

Query database I encapsulated a DataBase class: find ()

==== Start writing test

First, I need to test the Switchclothes class, but in this category, I need to instantiated through this class. The database class uses the select method to query the database and find out whether I want pants or a skirt. So, it’s really too troublesome. I just want to test the logic of this method. What if the database hangs up? If the username does not exist, do I have to go to the database to create such a piece of data? Too much trouble and not good enough. If you need to test a method that includes updating data, do you really need to modify the data?

The stub has arrived gorgeously. Mom no longer has to worry about me operating the database, nor does she have to worry about the interface being incomprehensible.

First introduction to PHPunit stub simulation return data

I can stub this class. To put it more simply, I think it is a simulation of this class and a fake database class;

   It is as above A=switchClothes B=database class D=database C=stub The class

   It should be called by A B, B queries the database.

  But the existence of C is the red line. C will not check the database. C is under my control. I can specify the find() method inside to return 1 or 0, at least in A seems to be the same as B. Anyway, it will return 0 or 1 to me. This means that C isolates the system A from B and D, reducing coupling;

   Then, I can start constructing the C I need.

  

<?php
use PHPUnit\Framework\TestCase;

class StubTest extends TestCase
{
    

    public function testStub()
    {
        // 为database类建立桩件。
        $stub = $this->getMockBuilder("database")//类名
                             ->setMethods(array(&#39;find&#39;)) //可以是多个方法
                              ->getMock();

        // 配置桩件。
        $stub->method(&#39;find&#39;)//想要设置返回值的方法
             ->willReturn(0);//设置返回值 

        // 现在调用 $stub->select() 将返回 &#39;裙子&#39;。
        $this->assertEquals(&#39;裙子&#39;, $stub->find());
    }
}
?>
Copy after login

This is C.

When taking a single test, just take the red path.

all


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template