2

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

我试图消除只是彼此正面/负面的多个步骤。

因此,而不是像这样的 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

有一个更好的方法吗?

4

3 回答 3

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
于 2012-10-27T17:59:28.923 回答
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
于 2014-03-07T12:04:19.263 回答
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
于 2014-12-05T11:20:43.993 回答