简体   繁体   中英

How would I structure this SpecFlow Feature/Scenario set correctly?

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 When step. 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 repeating Given steps. Same with outcomes.

Consider these scenarios for bowling game:

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

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.

[Given(@"a new bowling game")]
public void GivenANewBowlingGame()
{
    _game = new Game();
}

Also you can use one step definition to verify your outcomes (because they are actually same - verifying total score):

[Then(@"my total score should be (\d+)")]
public void ThenMyTotalScoreShouldBe(int score)
{
    Assert.AreEqual(score, _game.Score);
}

You are testing two scenarios and this is a valid way of doing it. Though there is one more way to do similar stuff:

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|

If you have the exact same step in two separate step classes, you'll need to define [Scope] , otherwise you'll get ambiguous error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM