国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home 類庫下載 PHP類庫 First introduction to PHPunit stub simulation return data

First introduction to PHPunit stub simulation return data

Oct 09, 2016 am 11:12 AM

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 "褲子";
                }
            }
        }

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;)) //可以是多個(gè)方法
                              ->getMock();

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

        // 現(xiàn)在調(diào)用 $stub->select() 將返回 &#39;裙子&#39;。
        $this->assertEquals(&#39;裙子&#39;, $stub->find());
    }
}
?>

This is C.

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

all


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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1501
276