【问题标题】:Test lengthy form without re-writing all the fill_ins everytime测试冗长的表格,而无需每次都重写所有的 fill_ins
【发布时间】:2014-10-13 03:46:42
【问题描述】:

我有一个很长的表格,对于其中的 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

【问题讨论】:

    标签: testing ruby-on-rails-4 rspec capybara integration-testing


    【解决方案1】:

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

         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  
    

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

    【讨论】:

    • 我喜欢使用哈希的想法!挑战虽然不是每个领域都是fill_in,有些是check,有些甚至是page.execute_script。但是你让我想知道我是否可以在哈希中使用整行代码:使得哈希看起来像{code: "fill_in 'field1', with: 'passing data'", outcome: "error message" }。如果我这样做了,有没有办法让代码评估为一行代码而不是字符串?
    • 整个想法是保持代码简单并与测试数据组合分开。您可以拥有任何类型的字段,但将根据提供的数据采取行动。例如:要选择的复选框可以是以 Y/N 形式从 testdata 提供,可以保存在外部 csv/xml/text 文件中。
    • 我的想法只是使用数据驱动测试,使用数组并运行多次迭代。
    • 我将使用我认为的 eval,会跟进以确保它有效。是的,我遵循你的想法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 2014-02-21
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多