我正在学习使用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
我确信我可以找出一些讨厌的正则表达式来实现评估这些步骤,但我觉得必须有一些已经存在或更优雅的东西,我可以做。如何使用预先填充的表单字段中的数据来评估表单?
答案 0 :(得分:15)
看看features / step_definitions / webrat_steps.rb,以下步骤定义看起来就像您要找的那样:
Then /^the "([^\"]*)" field should contain "([^\"]*)"$/ do |field, value|
field_labeled(field).value.should =~ /#{value}/
end