Skip to content

Instantly share code, notes, and snippets.

@RyosukeKamei
Last active April 10, 2018 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyosukeKamei/e60ab8df4c4ab63c8e689e699be45343 to your computer and use it in GitHub Desktop.
Save RyosukeKamei/e60ab8df4c4ab63c8e689e699be45343 to your computer and use it in GitHub Desktop.
本当に理解しているか自信ないから、改めてTDD(テスト駆動開発)をやってみた~アジャイルでDevOpsなシステム構築実践~ ref: https://qiita.com/RyosukeKamei/items/a6b37444961305f5ba5d
//-- 試験区分をすべて取得
Route::get('/getAllExamination', 'ExaminationController@getAll');
$ docker-compose exec workspace php artisan make:test ExaminationTest
class ExaminationController extends Controller
{
/**
* すべての試験区分を取得
*
*
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\static[]
*/
public function getAll()
{
/*
* オブジェクトをreturnするとLaravelがJSONにしてくれる
*/
return Examination::all();
}
}
public function testExaminationGetAll()
{
/*
* レスポンスを取得
*/
$response = $this->json('GET', '/api/getAllExamination');
/*
* テストケース
* 1. ステータスコード200が返ること
*/
$response->assertStatus(200);
/*
* テストケース
* 2. 取得するJOSNを配列にすると1件以上存在すること(0件はありえない)
*/
$this->assertGreaterThanOrEqual(1, count(json_decode($response->getContent())));
}
$ ./vendor/bin/phpunit --filter=ExaminationTest
$ docker-compose exec workspace php artisan make:controller ExaminationController
$ docker-compose exec workspace ./vendor/bin/phpunit --filter=testExaminationGetAll
PHPUnit 6.5.4 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 619 ms, Memory: 12.00MB
OK (1 test, 3 assertions)
$ docker-compose exec workspace ./vendor/bin/phpunit --filter=testExaminationGetAll
PHPUnit 6.5.4 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 579 ms, Memory: 10.00MB
There was 1 failure:
1) Tests\Feature\ExaminationTest::testExaminationGetAll
Failed asserting that 0 is equal to 1 or is greater than 1.
/var/www/tests/Feature/ExaminationTest.php:44
FAILURES!
Tests: 1, Assertions: 3, Failures: 1.
$ docker-compose exec workspace ./vendor/bin/phpunit --filter=testExaminationGetAll
PHPUnit 6.5.4 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 553 ms, Memory: 10.00MB
OK (1 test, 1 assertion)
$ docker-compose exec workspace ./vendor/bin/phpunit --filter=testExaminationGetAll
PHPUnit 6.5.4 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 703 ms, Memory: 10.00MB
There was 1 failure:
1) Tests\Feature\ExaminationTest::testExaminationGetAll
Expected status code 200 but received 404.
Failed asserting that false is true.
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:78
/var/www/tests/Feature/ExaminationTest.php:30
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
$ php vendor/bin/phpunit --filter=visit_sample_page
$ php artisan make:test ExaminationTest
$ ./vendor/bin/phpunit --filter=testExaminationGetAll
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment