This is the time since the combination of PHPunit documents and Daniel's explanation, want to record their own learning knowledge, future reference supplement, improve the learning of things
We usually test the code in the company's business using a single measurement, and he will help you find a place where your little thoughts are not comprehensive enough. (although Daniel said that development can write a single test to write the implementation of the code, but now I feel that there is still a lot of distance from that)
Stub (Pile piece):
The practice of replacing an object with (optionally) a test alias that returns a configured return value is referred to as the top pile (stubbing)"-This is the top pile explanation given in the official document
Now come back to see me to understand, how to say? Give me a pear.
The premise of = =
What I want to test is this method: Switchclothes ($username)----Query the database by name, sex is 1, return pants, is 0, return to skirt;
<?PHPClass Switch{ Public functionSwitchclothes ($username){ $database=Newdatabase (); $gender=$databse->find ("id=$username"); if($gender==0){ returnSkirt; }Else{ returnPants; } } }
Querying databases I encapsulated a database class inside: Find ()
= = Start writing tests
The first thing I need to test is the Switchclothes class, but in this class I need to go through the instantiation of database this class with the Select method, query the databases to get whether I want pants or skirts. So,, really is too troublesome, I just want to test the logic of this method is good, in case the database hangs, in case the username does not exist, I should also specifically to the database to create such a data, too troublesome is not good enough. If you need to test the method that contains the updated data, do you really want to modify the data?
The stub is coming. Mother no longer have to worry about me to operate the database, no longer worry about the interface does not pass God horse.
I can get a pile on this class. Speaking of popular Point, I think it is a simulation of this class, did a fake database class;
such as the A=switchclothes b=database class d= database c=stub that class
It should have been a call to B,b query the database.
But C's presence on the red line, C will not go to the database, C is controlled by me, I can specify the inside of the Find () method to return 1 or 0 at least in a It looks like B, anyway, will return me a 0 or one. This means that C separates a from the b,d system, reducing the coupling;
Then, you can start building the c I need.
<?PHP Usephpunit\framework\testcase;classStubtestextendstestcase{ Public functionteststub () {//build a pile for the database class. $stub=$this->getmockbuilder ("Database")//class name->setmethods (Array(' Find '))//can be multiple methods-Getmock (); //configuration of the pile piece. $stub->method (' Find ')//method to set the return value->willreturn (0);//Set return value//now call $stub->select () will return ' skirt '. $this->assertequals (' Skirt ',$stub-find ()); }}?>
This is C.
When you are alone, take the red route.
All
Primary knowledge PHPunit stub simulation return data