簡體   English   中英

如何正確構造此SpecFlow功能/場景集?

[英]How would I structure this SpecFlow Feature/Scenario set correctly?

這就是我所擁有的

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種情況下正確設置此設置,或者我看錯了嗎?

這是Step的定義,但它們對我來說似乎不合適,因為我必須將所有這些都放在同一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 = "mark@whatever.com";
      }

      [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 = "markl@whatever.com";
      }

      [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.");
      }
   }

在這些情況下,您具有不同的上下文。 當同一事件在不同背景下發生時,您會有不同的結果。 那就是您在這些方案中描述的內容。

因此,重復“ 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);
}

您正在測試兩種情況,這是一種有效的方法。 盡管還有另一種方法可以做類似的事情:

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] ,否則會出現模棱兩可的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM