2010-01-22 71 views
1

我刚刚开始练习使用GWT方法对下面的代码施加BDD,并且意识到我不能做第二次测试。如何使用MSpec与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?

回答

2

你会想要反转两个Then子句,因为它们基本上形成了不同的上下文类,在这个上下文类中被执行。当你阅读两个Then陈述时,你可以看到“if is not blank”和“is blank”形成了两个上下文。

  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 
        }; 
    } 
    

考虑,你可以有多个可能的 “空回答” 值(nullString.Empty" "\r\n),你可以写上下文f或任何这些。我常常不写标准值的任何可以想象的组合,而是

  • 有“快乐路径”一个方面,即响应不是空
  • 对空回复
一个上下文

根据你的例子,有人可能会认为Query类不适合判断答复是否满足“不空”规范。您应该在单独的课程中编写该决定,并且应该依赖该决定。这会带来一些优势:

  • 的关注点分离:在Query类和规范可发展分别
  • 可以重新使用在多个地方的规范,用户上岗前提示一个问题的答复他空回复
  • 你可以得到被测规范分开,而不必担心用户通知和数据库存储,从而防止爆炸的背景下为Query涉及