2017-01-03 8 views
0

単体テストの際にはどうしたらよいですか?どのようなステップ?どのような場合ですか?関数ごとにいくつのテストがありますか?単体テストを行う際に何を考慮する必要がありますか?

あなたの経験に関する情報もありがとうございます。また、私はlaravel phpunitで作業しています。私は例を行なったし、それが働いた:

public function test_for_clientUser() { 
    $this->json('POST', 'clientUser', ['id'=>'232421'])->seeJsonStructure([[]]); 
} 

私はidのリクエストを送信し、それが配列を返します。この試験に何を追加するのですか?

+0

テストでは、想定した結果を確認し、データを取得しないと仮定しています。応答はjson構造であるか、応答が成功すると仮定します。あなたは応答の特定をチェックしません。あなたは**論理**をテストし、データはテストしません。 Laravelの詳細については、[こちら](https://laravel.com/docs/5.0/testing)をご覧ください。 – Peon

答えて

0

コントローラをコントローラごとに複数のアクション(リスト、ストア、表示、更新、削除など)に分けることができます。

public function testStoringCityAsOwnerWithNotExistingCountryId() 
{ 
    $input = [ 
     'country_id' => 0, 
     'translations' => [ 
      [ 
       'name' => 'Варна', 
       'lang' => 'bg' 
      ] 
     ] 
    ]; 

    $response = [ 
     'errors' => [ 
      'country_id' => [ 
       trans(
        'validation.exists', 
        ['attribute' => 'country id'] 
       ) 
      ] 
     ] 
    ]; 

    $this->asOwner() 
     ->post('/cities', $input, $this->headers) 
     ->seeStatusCode(ResponseCode::PERMISSIONS_DENIED) 
     ->dontSeeJson(); 
} 

また、あなたがあなたのリスト情報、ページネーションとあなたはバグのように見つけることができる多くの他の多くの例 をテストすることができます。あなたには、いくつかのポストフォームのご入力検証をテストすることができる。例えば

実際に穴の概念は、すべてのバグに新しいテストを書く必要があるということです!

0

ルーメンプログラミングガイドから)この例によると: https://github.com/Apress/lumen-programming-guide/blob/master/tests/app/Http/Controllers/BooksControllerTest.php

あなたは、多かれ少なかれ、このテストになります。あなたがDBを変更していると

GET /index 
    - status code is 200 
    - returns a collection of (well-formed) records 

GET /show 
    - status code is 200 
    - returns a valid (well-formed) resource 
    - should fail with a non existing id (404) 
    - should not respond with 200 if id is not numeric. Maybe 404 

POST /store 
    - the resource stores in DB 
    - returns code 201 CREATED 
    - returns a valid json qith a resource id 

PUT /update 
    - status code is 204 
    - the resource has not the new value in DB 
    - the resource now has updated data in DB 
    - modified date was updated 
    - should fail with a non existing id (404) 
    - should not respond with 204 if id is not numeric. Maybe 404 

DELETE /destroy 
    - returns 204 
    - should fail with a non existing id (404) 
    - should not respond with 204 if id is not numeric. Maybe 404 

を(必要以上メモリ上で実行されているSQLiteインスタンスのようにDBをテストする)、これらは単体テストではなく機能的かもしれません。私は保証することはできません。著者はアクセプタンステストと呼んでいますが、ホワイトボックステスト(DBを直接操作する)なのではありません。

関連する問題