Here’s what I have
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
The problem is that I’m repeating the When twice, but I don’t see a way around this, what I need to do is figure out how you would set this up correctly with 2 scenarios or am I not looking at this correctly?
Here are the Step definitions, but they don’t seem right to me because I have to have all of these in the same Steps class for it to run. Doesn’t read correctly in my opinion. When I break these 2 apart and put them in their own step class I get the 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.");
}
}
You have different contexts in these scenarios. When same event occurs in different contexts you have different outcomes. Thats what you are describing by these scenarios.
So, there is no problem with repeating
Whenstep. You actually do same thing twice. Just reuse it. If in some scenarios you will have same contexts, but different events, then you will have repeatingGivensteps. Same with outcomes.Consider these scenarios for bowling game:
These scenarios have same context
Given a new bowling game. You should write same code to setup scene for each scenario. So, you have only one implementation of this step, which is reused by both scenarios.Also you can use one step definition to verify your outcomes (because they are actually same – verifying total score):