我将如何正确构建此规范流功能/场景集
本文关键字:功能 范流 何正确 构建 | 更新日期: 2023-09-27 18:32:19
这是我所拥有的
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 种场景正确设置它,还是我没有正确查看这个?
以下是步骤定义,但它们对我来说似乎不对,因为我必须将所有这些定义放在同一个步骤类中才能运行。 在我看来没有正确阅读。 当我将这两个分开并将它们放在自己的步骤类中时,我得到的是 erorr:
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]
,否则将出现歧义错误。