2014-09-04 112 views
1

有什麼方法可以替換已傳遞給其相應步驟定義的小黃瓜參數文本?替換黃瓜參數字符串

例如,假設我有變換的選配以下小黃瓜步:

Given I select "any option" from the "Choose an option" dropdown 

DROPDOWN_OPTION = Transform /^any option$/ do |str| 
    str = all("li", :minimum => 1).sample.text 
end 

如果選擇了小李的「設置」,我想執行這一步驟看起來像這樣之後的結果:

Given I select "settings" from the "Choose an option" dropdown 

我意識到,這不是寫小黃瓜(甚至在一般的測試,對於這個問題),但它是不幸的,我套牢了限制的理想方法。

任何人都可以建議嗎?

+1

不知道我明白,因爲'黃瓜'是如何工作的。 [步驟定義](http://cukes.info/step-definitions.html)一致使用正則表達式,因爲它們的真正意圖是允許這種類型的行爲。 – engineersmnky 2014-09-04 14:54:24

+0

也許它只是不適合我嗎? 我最初有這套作爲變換。類似於: 'DROPDOWN_OPTION =轉換/ ^任意選項$/do | str | STR =所有(「裏」,:最小=> 1)。樣品 end' 但是被執行此步驟時,結果仍顯示爲: '鑑於我選擇「的任何選項」的「選擇選項「下拉列表」 而不是: '鑑於我從「選擇選項」下拉列表中選擇了「選定的選項」 – 2014-09-04 16:21:24

回答

1

這是你問

/features/step_definitions/drop_down_steps.rb

Given /^I select (\w+) from the Choose an option dropdown$/ do |opt| 
    #do what ever you need to here with opt 
    instance_variable_set("@option_selected",DropDownOption.new(opt)) 
end 
When /^I make it go$/ do 
    #make it go 
    @option_selected.go 
end 
Then /^I expect that it went$/ do 
    #test that it went with the opt selected 
    @option_selected.went? 
end 
Then /^I expect it is still (\w+)$/ do |opt| 
    @option_selected.selected == opt 
end 

/features/drop_down_option.feature

Feature: DropDownOption 
    This is to test that I can send a DropDownOption away 
    but it will still be itself 

    Scenario: Selected Stuff 
    Given I select stuff from the Choose an option dropdown 
    When I make it go 
    Then I expect that it went 
    Then I expect it is still stuff 

這樣做是什麼每個匹配組傳遞到塊之下,允許您設置之類的實例變量,並對其執行操作。因此,在我的第一步中,它會從「stuff」正則表達式匹配中創建一個名爲@option_selected的實例變量。第二步告訴這個實例變量爲#go,第三步確保它爲#went?。最後,第四步確保即使它消失,它仍然是相同的「選項」。

這顯然是一個非常通用的例子,只是爲了展示功能和步驟定義的工作方式。它假設了很多東西,但基本上它會用類,如果你想改變的東西,那麼它必須作爲一個捕獲組

Feature: RandomString 
    Scenario: With a Random String 
    Given I type "random string" into the title field 
    Then I want it to be a String 

Transform /^I type ? "(.*)"$/ do |str| 
    rand(36**15).to_s(36) 
end 
Given /^(I type "(?:.*)") into the title field$/ do |str| 
    instance_variable_set("@random_string",str) 
end 
Then /^I want it to be a String$/ do 
    puts @random_string 
    expect(@random_string).to be_kind_of(String) 
end 
像這樣工作

class DropDownOption 
    def initialize(opt) 
    @opt = opt 
    @is_here = true 
    end 
    def go 
    @is_here = false 
    end 
    def selected 
    @opt 
    end 
    def is_here? 
    @is_here 
    end 
    def went? 
    !is_here? 
    end 
end 

更新

輸出

Feature: RandomString 

    Scenario: With a Random String      # features\random_string.feature:2 
    Given I type "random string" into the title field # features/step_definitions/random_steps.rb:4 
    Then I want it to be a String     # features/step_definitions/random_steps.rb:7 
    qxv75si91k2u10s #this is the transformed string it is random but will not alter the feature step definition 

Whe捕獲組匹配一個轉換將發生的變換器,這將被用來代替原始捕獲。

功能定義旨在爲測試目的而修復。他們的意思是措辭清晰,並由匹配者處理。它們並不意味着要被動態地實現,因爲這會帶走它們的真正目的。

+0

這並不完全是我所問的,可悲的是(儘管......我實際上很喜歡你在這裏所做的一切,並有幾個我想要應用的區域;這幾乎不浪費時間)。 雖然我只是稍微重構了我的問題以更具體一些。我所問的基本上是,如果小黃瓜是這樣的話:'鑑於我有一個隨機字符串'',我怎麼能打印出'鑑於我有'dj3(42 ^&e6he @ 3「'在結果? – 2014-09-04 17:21:53

+0

即如何 – 2014-09-04 17:26:18

+0

@DavidAdams我認爲我現在理解你的概念,但是你希望它變成什麼樣的? – engineersmnky 2014-09-04 17:57:05

0

雖然,黃瓜並不關心關鍵字的順序,慣例是根據下面寫你的步驟:

Given the context 
When action 
Then expectation 

因此改變你的情況我會寫:

Given I have options 
When I choose "whatever" dropdown 
Then I expect ... 
+0

其實,這是我們的初始方法。我把這個團隊設置成像這樣寫Gherkin - 但不幸的是,這種方法不利於我們組織的內部結構。手動測試人員(而不是開發人員甚至自動化團隊)是這些自動化測試的最終恩人,所以小黃瓜不得不被重寫爲更具可追溯性和特定性。 – 2014-09-04 17:09:59

1

而不是重寫你的源小黃瓜,它似乎是你的主要目標是真正的操縱你的結果,而不是你的輸入。如果這是您的目標,只需創建一個custom formatter。通過覆蓋AfterStep,您可以將值插入到結果中並傳達您所需的內容。

+0

不錯,我甚至不知道這些。我通常只是使用RSpec,但我會看看這些都是關於什麼的。 – engineersmnky 2014-09-04 20:44:33

+0

呃,他們讓它看起來比實際上似乎更容易這是真棒,但感謝發佈,因爲我不會意識到有多少回叫可用 – engineersmnky 2014-09-04 21:06:48