2012-04-26 111 views
2

這裏就是我有我將如何正確構建此SpecFlow功能/場景設置?

Feature: Register a new customer 
    As a user 
    I need to be able to register myself 
    so that I can place orders 

Scenario: Register a new customer with Valid information 
    Given I fill in valid customer information 
    When I press submit 
    Then I should be notified that I'm registered 

Scenario: Register a new customer with Invalid information 
    Given I fill in invalid customer information 
    When I press submit 
    Then I should be notified it was invalid 

的問題是,我重複兩次當,但我沒有看到解決的辦法,我需要做的是找出你將如何進行此項設置正確使用2個場景或者我沒有正確看待這個?

以下是步驟定義,但它們對我來說似乎不太合適,因爲我必須將它們放在相同的Steps類中才能運行。在我看來,沒有正確閱讀。當我將這兩部分分開並將它們放在他們自己的步驟課程中時,我得到了這個錯誤:

binding error: Ambiguous step definitions found for step 'When I press submit': 



[Binding] 
    public class RegisterAValidCustomerSteps 
    { 
     private RegisterCustomerViewModel _registerCustomerVm; 

     [Given(@"I fill in valid customer information")] 
     public void GivenIFillInValidCustomerInformation() 
     { 
     // use the ViewModel to represent the User interacting with the View 
     _registerCustomerVm = new RegisterCustomerViewModel(); 
     _registerCustomerVm.FirstName = "Mark"; 
     _registerCustomerVm.LastName = "W"; 
     _registerCustomerVm.Email = "[email protected]"; 
     } 

     [Given(@"I fill in invalid customer information")] 
     public void GivenIFillInInvalidCustomerInformation() 
     { 
     // simulate possible invalid name by missing the Last Name 
     _registerCustomerVm = new RegisterCustomerViewModel(); 
     _registerCustomerVm.FirstName = "Mark"; 
     _registerCustomerVm.Email = "[email protected]"; 
     } 

     [When(@"I press submit")] 
     public void WhenIPressSubmit() 
     { 
     _registerCustomerVm.Submit(); 
     } 

     [Then(@"I should be notified that I'm registered")] 
     public void ThenIShouldBeAbleToPlaceOrders() 
     { 
     _registerCustomerVm.MessageText.ShouldBe("Success! Check your inbox for confirmation"); 
     } 

     [Then(@"I should be notified it was invalid")] 
     public void ThenIShouldBeNotifiedItWasInvalid() 
     { 
     _registerCustomerVm.MessageText.ShouldBe("Failure! Last Name can't be blank."); 
     } 
    } 

回答

3

在這些場景中您有不同的上下文。當同一事件發生在不同的環境中時,你會得到不同的結果。那就是你在這些場景中所描述的。

因此,重複When步驟沒有問題。你實際上做了兩次同樣的事情。只需重用它。如果在某些情況下您會有相同的上下文,但不同的事件,那麼您將有重複步驟Given。結果一樣。

考慮這些情景的保齡球遊戲:

Scenario: Gutter game 
    Given a new bowling game 
    When all balls landing in gutter 
    Then total score should be 0 

Scenario: All strikes 
    Given a new bowling game 
    When all rolls are strikes 
    Then total score should be 300 

這些方案具有同樣的背景下Given a new bowling game。您應該爲每個場景設置相同的代碼。所以,你只有一個這個步驟的實現,它被兩個場景重用。

[Given(@"a new bowling game")] 
public void GivenANewBowlingGame() 
{ 
    _game = new Game(); 
} 

你也可以使用一個步驟定義,以驗證您的結果(因爲它們實際上是相同的 - 驗證總分):

[Then(@"my total score should be (\d+)")] 
public void ThenMyTotalScoreShouldBe(int score) 
{ 
    Assert.AreEqual(score, _game.Score); 
} 
+0

好吧,對於我的情況,我應該真的有一個鑑於「鑑於我填寫我的註冊名稱和電子郵件」和2和聲明「和信息是有效的」,「信息是無效的」,「當我按提交「,然後2條語句正確?也是所有這些在一個步驟課內? – 2012-04-26 22:15:10

+0

其實你可以有一個給定的步驟'給我填寫註冊表格'與specflow表格描述輸入數據。但我寧願用兩個不同的步驟(如現在這樣)來描述輸入的數據類型(有效或無效)。在準備上下文之後,你有一個'當我提交註冊表單時'(我認爲它更接近業務,然後'當我按Submit時')。然後是兩個步驟,就像現在一樣。 – 2012-04-26 22:24:33

1

您在測試兩個場景,這是做的一個有效途徑它。雖然有做類似的東西多了一個辦法:

Scenario 1: valid 
Given I enter the following data: 
|Field 1| Field 2| 
|Valid| Valid| 

Scenario 1: invalid 
Given I enter the following data: 
|Field 1| Field 2| 
|Valid| Invalid| 

如果你有兩個單獨的步驟類完全相同的步驟中,您需要定義[Scope],否則你會得到明確的錯誤。

+0

何去何從? – 2012-04-26 21:48:43

+0

時間和然後保持在你的問題相同。 – 2012-04-30 13:45:08