[英]How to add integers to scenario and feature files from a table in specflow? Why do I get tan error
[英]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.