2

这就是我所拥有的

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

问题是我重复了 When 两次,但我没有看到解决方法,我需要做的是弄清楚如何在 2 个场景中正确设置它,还是我没有正确看待这个?

这是 Step 定义,但它们对我来说似乎不正确,因为我必须将所有这些都放在同一个 Steps 类中才能运行。在我看来阅读不正确。当我将这两个分开并将它们放在自己的 step 类中时,我得到了错误:

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

2 回答 2

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);
}
于 2012-04-26T21:38:39.107 回答
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],否则会出现模棱两可的错误。

于 2012-04-26T21:42:04.910 回答