【问题标题】:DRY up turnip step definitions干萝卜步骤定义
【发布时间】:2012-06-27 21:55:14
【问题描述】:

我知道这可以通过捕获可选组来使用 cucumber(请参阅此处的 tip 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

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

【问题讨论】:

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


    【解决方案1】:

    如何使用以下步骤定义:

    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
    

    【讨论】:

      【解决方案2】:

      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
      

      【讨论】:

        【解决方案3】:
        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-05-27
          • 1970-01-01
          • 1970-01-01
          • 2022-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多