Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel Testing¾ÙÐе¥Î»²âÊÔ£¿
Ëæ×Ålaravel¿ò¼ÜµÄÒ»Ö±Éú³¤£¬µ¥Î»²âÊÔÒ²³ÉΪÁËÏÖ´ú±à³ÌÖеÄÒ»¸ö²»¿É»òȱµÄ²¿·Ö¡£µ¥Î»²âÊÔ¿ÉÒÔÈ·±£ÎÒÃǵĴúÂëÔÚ²î±ðÔËÐÐÇéÐÎϾßÓÐÎȹ̵ÄÐÐΪ£¬´ó´ó½µµÍÁ˳ÌÐò·ºÆð¹ýʧµÄ¸ÅÂÊ¡£ÔÚlaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃlaravel testingÀ´¾ÙÐе¥Î»²âÊÔ¡£±¾ÆªÎÄÕ½«ÏÈÈÝÔõÑùʹÓÃlaravel testing¾ÙÐе¥Î»²âÊÔ¡£
×°ÖÃLaravel
ÔÚ¾ÙÐе¥Î»²âÊÔ֮ǰ£¬ÎÒÃÇÐèҪװÖÃÒ»¸öLaravelÏîÄ¿À´¾ÙÐвâÊÔ¡£¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´×°ÖÃLaravel:
composer create-project –prefer-dist laravel/laravel blog
½¨Éè²âÊÔÎļþ
ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÔËÐÐÒÔÏÂÏÂÁîÀ´½¨Éè²âÊÔÎļþ£º
php artisan make:test ExampleTest
Õ⽫ÔÚtestsĿ¼ÏÂÌìÉúÒ»¸öExampleTest.phpÎļþ¡£ÎÒÃÇ¿ÉÒÔÔÚÆäÖбàд²âÊÔÓÃÀý¡£
±àд²âÊÔÓÃÀý
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª±àд²âÊÔÓÃÀý¡£ÎÒÃÇ¿ÉÒÔÔÚExampleTestÖбàд²âÊÔÓÃÀý£º
public function testBasicTest() { $response = $this->get('/'); $response->assertStatus(200); }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ²âÊÔÓÃÀýÖУ¬ÎÒÃÇÏòÓ¦ÓóÌÐòÌᳫGETÇëÇ󣬲¢¼ì²éÏìӦ״̬ÊÇ·ñΪ200¡£
ÔËÐвâÊÔ
½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÔËÐвâÊÔ£º
php artisan test
ÈôÊÇËùÓеIJâÊÔÓÃÀý¶¼Í¨¹ýÁË£¬Õâ¸öÏÂÁ»á·µ»ØÒ»¸öÀֳɵÄ״̬¡£
¶ÏÑÔ
ÔÚ±àд²âÊÔÓÃÀýʱ£¬ÎÒÃÇ¿ÉÒÔʹÓöÏÑÔÀ´¼ì²é´úÂëµÄÐÐΪÊÇ·ñÇкÏÔ¤ÆÚ¡£Laravel TestingÌṩÁ˶àÖÖÀàÐ͵ĶÏÑÔ¡£
ÔÚÉÏÃæµÄÀý×ÓÖУ¬ÎÒÃÇʹÓÃÁËassertStatus(200)¶ÏÑÔ¡£Õâ¸ö¶ÏÑÔ»á¼ì²éÏìӦ״̬ÊÇ·ñΪ200¡£ÈôÊÇ״̬²»Îª200£¬²âÊÔ½«»áʧ°Ü¡£³ý´ËÖ®Í⣬ÉÐÓÐÐí¶àÆäËûµÄ¶ÏÑÔ¿ÉÒÔÓÃÀ´¾ÙÐвâÊÔ¡£
Ä£ÄâÇëÇó
ÔÚ²âÊÔÖУ¬Ä£ÄâÇëÇóºÍÏìÓ¦ÊÇÏ൱³£¼ûµÄ¡£Laravel TestingÌṩÁËÐí¶àÒªÁìÀ´Ä£ÄâÇëÇó¡£ÀýÈ磺
// GETÇëÇó $response = $this->get('/path'); // POSTÇëÇó $response = $this->post('/path', [ 'name' => 'value', ]); // PUTÇëÇó $response = $this->put('/path', [ 'name' => 'value', ]); // DELETEÇëÇó $response = $this->delete('/path');
µÇ¼ºó¸´ÖÆ
Êý¾ÝÌṩÕß
ÔÚ±àд²âÊÔÓÃÀýʱ£¬ÓÐʱÎÒÃÇÐèÒª²âÊÔ²î±ðµÄÊý¾Ý¼¯¡£ÎÒÃÇ¿ÉÒÔʹÓÃLaravel TestingµÄÊý¾ÝÌṩÕßÀ´ÊµÏÖÕâ¸öÄ¿µÄ¡£ÀýÈ磺
/** * @dataProvider additionProvider */ public function testAddition($a, $b, $expected) { $this->assertEquals($expected, $a + $b); } public function additionProvider() { return [ [0, 0, 0], [0, 1, 1], [1, 1, 2], ]; }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄÀý×ÓÖУ¬ÎÒÃÇʹÓÃÁËÊý¾ÝÌṩÕßÀ´²âÊÔ²î±ðµÄÊý¾Ý¼¯¡£
×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃLaravel Testing¾ÙÐе¥Î»²âÊÔ¡£µ¥Î»²âÊÔ¿ÉÒÔÈ·±£ÎÒÃǵĴúÂëÔÚ²î±ðÔËÐÐÇéÐÎϾßÓÐÎȹ̵ÄÐÐΪ£¬´ó´ó½µµÍÁ˳ÌÐò·ºÆð¹ýʧµÄ¸ÅÂÊ¡£ÔÚ±àд²âÊÔÓÃÀýʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃLaravel TestingÌṩµÄ¶àÖÖÀàÐ͵ĶÏÑÔÒÔ¼°Êý¾ÝÌṩÕßÀ´ÊµÏÖ²î±ðµÄ²âÊÔÐèÇó¡£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel Testing¾ÙÐе¥Î»²âÊÔ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡