2016-08-11 17 views
1

我有SpecFlow場景,我想指定一個可能的值範圍作爲輸入。如何實現SpecFlow輸入屬性的解析器

Scenario: Search completed successfully 
Given I am on the application screen 
    And I enter an number of AA191DD 
When I press the search button and wait till search will be completed 
Then I should see *AtLeastOne* number of cars available for given number 

實際上,我正試圖實現某種Times結構。 要求是要能夠進入某些範圍,如:

  • AtLeastOnce
  • 精確({數})

當前的實現是:我有編號枚舉:

public enum Number 
{ 
    AtLeastOnce, 
    None 
} 

並且Specflow綁定方法的輸入爲這個枚舉。

public void ThenIShouldSeeNumberOfCarsAvailable(Number numberEnum) 
{ 
} 

但現在我不知道如何實現Numbers.Exact(45)。 有什麼建議嗎?

回答

1

我覺得你有點複雜的事情,但我會嘗試給你一個工作解決方案,因爲我會這樣做,我認爲。

首先我不認爲你可以用enum來做,因爲你需要存儲額外的信息(要比較的確切數量),所以我可能會使用接口和實現。我可能會稱之爲INumericComparison或類似的。然後我會有一個'至少','沒有'和'確切'的實現。我會用靜態工廠方法實現一個類枚舉類來創建3種不同的類型。然後,我會在步驟中將StepArgumentTransformation轉換爲其中一種類型。我在我的手機中,所以代碼將不在手邊,但類似這樣的。

Public class NumericComparison 
{ 
    Public static INumericComparison None() 
    { 
     Return new ExactComparison(0); 
    } 
    Public static INumericComparison AtLeastOne() 
    { 
     Return new AtLeastComparison(1); 
    } 
    Public static INumericComparison Exactly(into value) 
    { 
     Return new ExactComparison(value); 
    } 
} 

Public interface INumericComparison 
{ 
    Public book PassesComparison(int valueToCompare); 
} 

我將離開實際的實現作爲學生的練習。

step參數轉換可以在specflow中使用正則表達式,所以您應該能夠想出一些匹配每個文本位的方法,並且只返回一個靜態方法