This article is intended for beginners who have a certain understanding of the PHP and laravel framework and have read the Laravel introductory video: Laravel 5 Fundamentals. This article explains how to test your application using the modular test tool.
Related video Laravel test has been downloaded to the network disk if you haven't seen it, please download and watch it first:
- Links: Http://pan.baidu.com/s/1sjXeLQH
- Extract Password: jjb5
1. Under the tests directory, create a new test file ordertest.php.
Add ($product); er->add ($product 2); $this->assertequals (2,count (er->products ()));
Create a order.php file in the APP directory
Products[] = $product; } Public Function Products () { return $this->products;
Optimize the last line of code using the Assertcount () method.
Add ($product); er->add ($product 2); $this->assertcount (2, er->products ());
Input command: PHPUnit tests/unit test
2. Test order total.
Input command: PHPUnit--filter an_order_can_determine_the_total_cost_of_all_its_products
Test only one method
Add ($product); er->add ($product 2); $this->assertcount (2, er->products ()); } /** @test * /function an_order_can_determine_the_total_cost_of_all_its_products () { er = new Order; $product = new Product (' Fallout4 ', "); $product 2 = new Product (' Pillowcase ', 7); er->add ($product); er->add ($product 2); $this->assertequals (er->total ());
Test file order.php
Products[] = $product; } Public Function Products () { return $this->products; } Public Function Total () {total = 0; foreach ($this->productsas $product) { $total + = $product->cost (); } return $total;
Pass the test
Use the Createorderwithproducts () method to simplify your code.
Createorderwithproducts (); $this->assertcount (2, er->products ()); } /** @test * /function an_order_can_determine_the_total_cost_of_all_its_products () { er = $this Createorderwithproducts (); $this->assertequals (er->total ()); } protected function createorderwithproducts () { er = new Order; $product = new Product (' Fallout4 ', "); $product 2 = new Product (' Pillowcase ', 7); er->add ($product); er->add ($product 2); return er;
Summarize
This article slightly in-depth test the Order class, follow-up continue to elaborate.