2012-04-26 114 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