2012-04-26 21 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つのシナリオで正しく、または私はこれを正しく見ていないのですか?

ここにステップ定義がありますが、これらのすべてを同じステップクラスで実行する必要があるため、私は正しいとは思われません。私の意見では正しく読まない。私は離れてこれらの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 = "[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ステップ。あなたは実際に同じことを2回します。ちょうどそれを再使用してください。シナリオによっては同じコンテキストでイベントが異なる場合、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を持っています。シナリオごとにセットアップシーンに同じコードを書き込む必要があります。したがって、この手順の実装は1つしかなく、両方のシナリオで再利用されます。 (彼らは実際には同じであるため、 - 合計スコアを検証する)

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

また、あなたはあなたの成果を確認するために、1つのステップの定義を使用することができます。

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

私の場合、実際には「私は登録名とメールアドレスを記入してください」、2 And文は「情報は有効です」、「情報は無効です」、「 Submit "を実行し、2 Then Thenステートメントを修正しますか?これらはすべてステップクラスの中にありますか? –

+0

実際には、入力データを記述するspecflowテーブルを使って、「記入フォームを記入してください」というステップを指定することができます。しかし、私はむしろ、どんな種類のデータが入力されたか(有効か無効か)を記述する2つの異なるステップ(あなたが今のように)に行きたいと思います。コンテクストを準備した後、あなたは「私が登録フォームを提出すると、私はビジネスに近いと思います。そして2つのステップは、あなたが今持っているように。 –

1

あなたは2つのシナリオをテストしていると、これはやっての有効な方法でありますそれ。同様のものを行うためのもう一つの方法がありますが:

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| 

次の2つの別々のステップのクラスでまったく同じステップを持っている場合は、あなたがそうでなければ、あなたがあいまいなエラーが発生します、[Scope]を定義する必要があります。

+0

どこに行くのですか? –

+0

WhenとThenはあなたの質問と同じままです。 –

関連する問題