【问题标题】:How do I check that a form is pre-populated with values using Cucumber and Webrat?如何使用 Cucumber 和 Webrat 检查表单是否预先填充了值?
【发布时间】:2010-11-30 15:36:09
【问题描述】:

我正在使用 Rails 学习 Cucumber 和 Webrat,希望获得一些关于测试“编辑”表单的最佳方法的建议。当我浏览到用户的个人资料时,我会看到一个编辑表单,其中用户的信息预先填充在表单字段中。我希望能够测试这些字段是否确实包含我期望的信息。这是我的场景:

  Scenario: View My Profile
    Given  I am logged in as "Mike" with password "secret"
    When I go to my profile page
    Then I should see "Mike" in the "Login" field
    And I should see "mike@email.com" in the "Email" field
    And I should see a blank "Password" field
    And I should see a blank "Password confirmation" field

Cucumber 正确地告诉我,我需要定义以下自定义步骤:

Then /^I should see "([^\"]*)" in the "([^\"]*)" field$/ do |arg1, arg2|
  pending
end

Then /^I should see a blank "([^\"]*)" field$/ do |arg1|
  pending
end

我确信我可以找出一些讨厌的正则表达式来实施评估这些步骤,但我觉得肯定有一些已经存在或更优雅的东西我可以做。您如何评估表单字段中预先填充了数据的表单?

【问题讨论】:

    标签: ruby-on-rails regex cucumber bdd webrat


    【解决方案1】:

    查看 features/step_definitions/webrat_steps.rb,以下步骤定义看起来像您要查找的内容:

    Then /^the "([^\"]*)" field should contain "([^\"]*)"$/ do |field, value|
      field_labeled(field).value.should =~ /#{value}/
    end
    

    【讨论】:

    • 当。我不知道我是怎么错过的!基于此,我创建了一个适用于空字段的版本。在 /^ 中,“([^\"]*)” 字段应该是空白$/”,被实现为“field_labeled(field).value.should == nil”。谢谢!结束`
    猜你喜欢
    • 1970-01-01
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 2017-03-28
    • 2012-05-17
    • 1970-01-01
    • 2016-07-13
    相关资源
    最近更新 更多