如何使用 Cucumber 和 Webrat 检查表单是否已预先填充值?

发布于 2024-08-06 08:36:54 字数 926 浏览 5 评论 0原文

我正在使用 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 "[email protected]" 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

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

I am learning Cucumber and Webrat with Rails and would like some advice on the best way to test an "edit" form. When I browse to a user's profile I am presented with an edit form with the user's information pre-populated in the form fields. I would like to be able to test that the fields do in fact contain the information I expect. Here's my scenario:

  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 "[email protected]" in the "Email" field
    And I should see a blank "Password" field
    And I should see a blank "Password confirmation" field

Cucumber tells me, correctly, that I need to define the following custom steps:

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

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

I'm sure I can figure out some nasty regex to implement evaluating these steps, but I feel there must be something already existing or more elegant that I can do. How do you evaluate forms with data pre-populated in the form fields?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅淡淡 2024-08-13 08:36:54

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

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

Have a look at features/step_definitions/webrat_steps.rb, the following step definition looks like what you are looking for:

Then /^the "([^\"]*)" field should contain "([^\"]*)"$/ do |field, value|
  field_labeled(field).value.should =~ /#{value}/
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文