1

次のコードの実行に対する GWT アプローチを使用して BDD の練習を始めたところですが、2 番目のテストを実行できないことに気付きました。

私のGWTは次のようになります

Given there exists an open query
When the user replies to the query
Then it should save the reply if the reply is not blank

次に、ユーザーに通知し、空白の場合は返信を保存しないでください

だから私はそれをそのようにコーディングしました

public class when_user_replies_to_the_query : OpenQuery
{
    Because
    {
         query.Reply(data);
    }

    ThenIt should_save_the_reply_to_the_database_if_there_is_a_reply

    ThenIt should_notify_the_user_if_there_is_no_text_in_the_reply_and_not_save_to_database
}

public class Query
{
    void Reply(string data)
    {
        //do something
    }
}

しかし、最初のケースではデータに何かが含まれている必要があり、2 番目のケースではデータが空の文字列である必要があるため、2 番目のケースは実行できないことに気付きました。

これは、GWTを次のようなものに分割する必要があることを意味しますか?

Given the reply is blank
When the user replies to the query
Then it should notify the user ......

これが事実である場合、私はリターンのために大量のnullケースのシナリオを書いているでしょう

values being null. Such as
Given the database is null
When retrieving queries
Should reply with error message
When saving queries
Should save to file and reply with error message
When // basically doing anything
Should //give appropriate response

これは、BDD仕様をどのように書くべきですか? そして、私は適切なフォーラム O_O にいますか?

4

1 に答える 1

2

Then2つの句は基本的に異なるコンテキストを形成し、その下でQueryクラスが実行されるため、2つの句を逆にする必要があります。両方のステートメントを読むThenと、「空白でない場合」と「空白の場合」が両方のコンテキストを形成していることがわかります。

  1. コンテキスト#1:

    Given an open query
    Given a non-blank reply
    When the user replies to the query
    It should save the reply
    
    public class When_the_user_replies_to_the_query_and_the_reply_is_not_blank
    {
       static Query Query;
    
       Establish context = () =>
       {
           Query = new Query();
       };
    
       Because of = () =>
       {
           Query.Reply("answer");
       };
    
       It should_save_the_reply = () =>
       {
           // Use your imagination
       };
    }
    
  2. コンテキスト#2:

    Given an open query
    Given a blank reply
    When the user replies to the query
    It should not save the reply
    It should notify the user
    
    public class When_the_user_replies_to_the_query_and_the_reply_is_blank
    {
       static Query Query;
    
       Establish context = () =>
       {
           Query = new Query();
       };
    
       Because of = () =>
       {
           Query.Reply(String.Empty);
       };
    
       It should_not_save_the_reply = () =>
       {
           // Use your imagination
       };
    
       It should_notify_the_user = () =>
       {
           // Use your imagination
       };
    }
    

複数の可能な「空の応答」値(、、、)を持つ可能性があることを考慮するとnull、これらString.Emptyのいずれかのコンテキストを記述できます。私はしばしば、考えられる値の組み合わせの仕様を作成しませんが、むしろ" "\r\n

  • 「ハッピーパス」のコンテキストが1つあります。つまり、返信が空ではありません。
  • 空の返信に対して1つのコンテキストがあります

Queryあなたの例に照らして、応答が「空ではない」仕様を満たすかどうかを決定するためのクラスは適切な場所ではないと主張することができます。むしろ、その決定を別のクラスにコード化する必要があり、Queryその決定に依存する必要があります。これにはいくつかの利点があります。

  • 関心の分離:Queryクラスと仕様は別々に進化する可能性があります
  • 仕様を複数の場所で再利用でき、ユーザーが空の返信を投稿する前に返信に問題があることを示唆します
  • ユーザーへの通知やデータベースの保存を気にすることなく、テスト対象の仕様を個別に取得できるため、Query懸念事項によるコンテキストの急増を防ぐことができます。
于 2010-01-22T13:32:30.330 に答える