TDD聲稱我應該先寫測試。假設我想寫一個服務,如果我還沒有任何東西,我該如何開始爲服務編寫測試?什麼是第一個測試?嘗試實例化服務並獲得異常?測試不存在的代碼
基於Laravel的PHP例子:
class ServiceTest extends TestCase
{
public function testServiceExists()
{
$service = App::make('grid');
}
}
結果:
PHPUnit 5.5.4 by Sebastian Bergmann and contributors.
E 1 /1 (100%)
Time: 123 ms, Memory: 14.00MB
There was 1 error:
1) ServiceTest::testServiceExists ReflectionException: Class grid does not exist
/home/supertrall/domains/md.local/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php:749 /home/supertrall/domains/md.local/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php:644 /home/supertrall/domains/md.local/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:709 /home/supertrall/domains/md.local/laravel/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:237 /home/supertrall/domains/md.local/laravel/tests/Grid/ServiceTest.php:20
ERRORS! Tests: 1, Assertions: 0, Errors: 1.