干掉萝卜步骤定义

时间:2012-06-27 21:55:14

标签: ruby-on-rails testing rspec cucumber dry

我知道这可以通过捕获可选组来使用黄瓜(请参阅此处tip 3),并且我在萝卜中使用它,但我不喜欢这个解决方案。

我正在尝试消除彼此正面/负面的多个步骤。

所以不是像这样的两个步骤:

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

有更好的方法吗?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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