测试冗长的表单,而不是每次重写所有fill_ins

时间:2014-10-13 03:46:42

标签: 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: "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

1 个答案:

答案 0 :(得分:0)

在这种情况下,我建议编写如下的数据驱动测试

     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  

这样,所有组合都将在测试数据中定义,而不是在代码中定义,所以将来如果没有。验证甚至增加或减少你不必触摸代码。只有数据。 我希望将溶液干燥是有意义的。