我刚刚开始使用GWT方法来练习BDD,以实现以下代码施加,只是意识到我无法进行第二次测试。

我的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
    }
}

但是后来我意识到我无法做第二种情况,因为第一种情况需要数据在其中有东西,而第二种情况则说数据应该是一个空字符串。

这是否意味着我应该将我的gwt分成类似

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

如果是这种情况,那么我会写大量的无效案例以返回

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吗?

有帮助吗?

解决方案

你要倒转两个 Then 条款,因为它们基本上形成了不同的上下文 Query 班级运动。当你读到两者 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),您可以为其中任何一个编写上下文。我通常不为任何可想象的值组合编写规格,而是

  • 有一个“快乐之路”的上下文,即回复不是空的
  • 有一个空白的上下文

根据您的榜样,有人可能会说 Query 班级不是决定答复是否满足“不是空”规范的正确位置。您应该在单独的类和 Query 应该依靠那个决定。这将带来一些优势:

  • 关注的分离: Query 班级和规范可以单独进化
  • 您可以在多个位置重复使用规范,在用户发布其空回复之前暗示答复问题
  • 您可以单独进行测试的规范,而不必担心用户通知和数据库保存,从而防止上下文爆炸 Query 关注
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top