2012-06-27 45 views
2

我知道这是可能的使用黄瓜通过捕获可选组(见tip 3这里),我有它在萝卜工作,但我不喜欢的解决方案。DRY向上萝卜步骤定义

我想消除多个步骤,只是彼此的积极/消极。

因此,而不是2个步骤是这样的:

step "I should see :content in the footer" do |content| 
    within(".footer-main") do 
    page.should(have_selector("h3", text: content)) 
    end 
end 

step "I should not see :content in the footer" do |content| 
    within(".footer-main") do 
    page.should_not(have_selector("h3", text: content)) 
    end 
end 

我可以这样做:

step "I should :not_text see :content in the footer" do |not_text, content| 
    within(".footer-main") do 
    not_text.blank? ? page.should(have_selector("h3", text: content)) : page.should_not(have_selector("h3", text: content)) 
    end 
end 

这工作得很好,但我真的不喜欢的事情是,我必须把在正面情况下的空括号如下:

Scenario: User should see Company in the footer 
    When I visit the "root page" 
    Then I should "" see "Company" in the footer 

有没有更好的方法来做到这一点?

回答

0

如何使用以下步骤定义:

step "I :should_or_not_text see :content in the footer" do |should_or_not_text, content| 
    within(".footer-main") do 
    should_or_not_text == "should" ? page.should(have_selector("h3", text: content)) : page.should_not(have_selector("h3", text: content)) 
    end 
end 

,那么该功能看起来一点点清洁:

Scenario: User should see Company in the footer 
    When I visit the "root page" 
    Then I "should" see "Company" in the footer 
0

AFAIK - 这应该工作:

step "I should (not)? see :content in the footer" do |not_text, content| 
    within(".footer-main") do 
    not_text.blank? ? page.should(have_selector("h3", text: content)) : page.should_not(have_selector("h3", text: content)) 
    end 
end 

然后:

Scenario: User should see Company in the footer 
    When I visit the "root page" 
    Then I should see "Company" in the footer 

Scenario: User should not see City in the footer 
    When I visit the "root page" 
    Then I should not see "City" in the footer 
0
step 'I should:not see :text' do |negative, text| 
    expect(page).to(negative ? have_no_text(text) : have_text(text)) 
end 

placeholder(:not) do 
    match(/ not/) do 
    true 
    end 
end