10

我正在使用 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

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

4

1 回答 1

15

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

Then /^the "([^\"]*)" field should contain "([^\"]*)"$/ do |field, value|
  field_labeled(field).value.should =~ /#{value}/
end
于 2009-09-23T09:46:41.340 回答