Testing 测试冗长的表单,而无需每次重新编写所有填写内容
我有一个很长的表单,对于其中的3个字段,我需要分别测试这些字段的填写是否正确,因为错误会导致非常不同的操作。但是,编写这样的集成测试非常单调乏味:Testing 测试冗长的表单,而无需每次重新编写所有填写内容,testing,ruby-on-rails-4,rspec,capybara,integration-testing,Testing,Ruby On Rails 4,Rspec,Capybara,Integration Testing,我有一个很长的表单,对于其中的3个字段,我需要分别测试这些字段的填写是否正确,因为错误会导致非常不同的操作。但是,编写这样的集成测试非常单调乏味: # Test 1 describe "test where field3 fails" do before do fill_in "field1", with: "passing info" fill_in "field2", with: "passing info" fill_in "field3", with: "
# Test 1
describe "test where field3 fails" do
before do
fill_in "field1", with: "passing info"
fill_in "field2", with: "passing info"
fill_in "field3", with: "not passing info"
...
end
it "should lead to an error specific to field3" do
...
end
end
# Test 2
describe "test where field2 fails" do
before do
fill_in "field1", with: "passing info"
fill_in "field2", with: "not passing info"
fill_in "field3", with: "passing info"
...
end
it "should lead to an error specific to field2" do
...
end
end
# Test 3
describe "test where field1 fails" do
before do
fill_in "field1", with: "not passing info"
fill_in "field2", with: "passing info"
fill_in "field3", with: "passing info"
...
end
it "should lead to an error specific to field1" do
...
end
end
describe "test all form fields" do |data|
before do
fill_in "field1", with: data[0]
fill_in "field2", with: data[1]
fill_in "field3", with: data[2]
...
end
it "should lead to an error message" do |CorrespondingErrorMsg|
...
end
在这种情况下,我建议编写如下的数据驱动测试
describe "test all form fields" do |data|
before do
fill_in "field1", with: data[0]
fill_in "field2", with: data[1]
fill_in "field3", with: data[2]
...
end
it "should lead to an error message" do |CorrespondingErrorMsg|
...
end
这样,所有的组合都将在测试数据中定义,而不是在代码中定义,因此在将来,如果验证数量增加或减少,您不必接触代码,只需接触数据。
我希望使溶液干燥是有意义的。我喜欢使用散列的想法!尽管并非每个字段都是
填写
,有些是检查
,有些甚至是页面。执行脚本
。但是你让我想知道我是否可以在散列中找到整行代码:这样散列看起来像{code:“在'field1'中填充:'传递数据'”,结果:“错误消息”}
。如果我这样做,有没有办法让代码作为一行代码而不是一个字符串进行计算?整个想法是保持代码简单,并与测试数据组合分开。您可以有任何类型的字段,但操作将根据提供的数据进行。例如:要选择的复选框可以作为testdata的Y/N提供,testdata可以保存在外部数据中csv/xml/text文件。我的想法只是使用数据驱动的测试,使用数组并运行多个迭代。我将使用一个eval,我认为,它将跟进以确保工作正常。是的,我遵循你的想法
describe "test all form fields" do |data|
before do
fill_in "field1", with: data[0]
fill_in "field2", with: data[1]
fill_in "field3", with: data[2]
...
end
it "should lead to an error message" do |CorrespondingErrorMsg|
...
end