【问题标题】:Cucumber - And I should see "first" before "last" - Rails 3.1Cucumber - 我应该在“last”之前看到“first” - Rails 3.1
【发布时间】:2011-09-30 15:28:02
【问题描述】:

[注意:使用 Rails 3.1。]

我正在尝试测试多个模型是否以正确的顺序显示在我的页面上,在我的情况下,按 desc 日期显示,最新的在顶部。

我知道我可以执行以下操作来检查页面上是否存在某些内容:

And I should see "Some content on my page."

但我想做一些类似的事情:

And I should see "Most Recent" before "Really old"

我将如何编写步骤来做到这一点?我相信“我应该看到”只是扫描整个页面以查找指定的参数,只是不确定如何处理正确的顺序。

提前致谢!

【问题讨论】:

    标签: testing cucumber ruby-on-rails-3.1 integration-testing


    【解决方案1】:

    使用 page.body 上的String#index 查找每个位置,并断言 first

    And /^I should see "([^"]*)" before "([^"]*)"$/ do |phrase_1, phrase_2|
      first_position = page.body.index(phrase_1)
      second_position = page.body.index(phrase_2)
      first_position.should < second_position
    end
    

    【讨论】:

    • 你能再澄清一下吗?
    【解决方案2】:

    更扁平的版本,带有 MiniTest 断言:

    And /^I should see "([^"]*)" before "([^"]*)"$/ do |phrase_1, phrase_2|
      assert page.body.index(phrase_1) < page.body.index(phrase_2)
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-21
      • 2018-01-26
      • 2022-01-14
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多