如何用Capybara& amp ;;测试多步形式? MINITEST?

时间:2015-03-05 20:03:45

标签: ruby testing sinatra capybara minitest

我有一个单页Sinatra应用程序,它有一个多步骤表单/向导界面。如果我想用Capybara测试表格,是否需要重复每个测试的所有步骤?我希望避免这样的事情:

it "visits the home page" do 
  vist "/"
  page.should have_content('foo')
end

it "clicks the first button" do 
  vist "/"
  page.should have_content('foo')
  button_click("Next")
end

it "clicks the second button" do 
  vist "/"
  page.should have_content('foo')
  button_click("Next")
  page.should_have_content('bar')
end

it "clicks the third button" do 
  vist "/"
  page.should have_content('foo')
  button_click("Next")
  page.should_have_content('bar')
  button_click("Next")
end

我发现了一篇关于nested tests using RSpec and Capybara的文章,但未能使用类似的技术与Minitest合作。

1 个答案:

答案 0 :(得分:1)

我已经为你做了一些研究,我会与你分享我发现的东西。

为此,您应考虑将Minitest测试“转换”为specs。这使您可以访问类似的语法,RSpec的 - describe具有嵌套功能。

我将简化代码以提供示例,但应该清楚将逻辑放在何处。

让我们看几个例子。

1)。让我们为Array做一些简单的测试:

require "minitest/autorun"

describe Array do
  before do
    @arr = []
  end

  describe "when initialized" do
    it "is empty" do
      @arr.length.must_equal 0
    end
  end
end

这应该通过,这里没有什么真正的幻想。

2)。让我们添加另一个describe,嵌套到我们的一个:

describe Array do
  before do
    @arr = []
  end

  describe "when initialized" do
    it "is empty" do
      @arr.length.must_equal 0
    end

    describe "when element added" do
      it "length reflects the change" do
        @arr << "a"
        @arr.length.must_equal 1
      end
    end
  end
end

这也有效 - 元素已添加到数组中,其length表示正确。

3)。让我们尝试嵌套另一个块。我们希望保留@arr << "a",所以如果我们添加另一个元素,@arr.length将是2.让我们看看:

describe Array do
  before do
    @arr = []
  end

  describe "when initialized" do
    it "is empty" do
      @arr.length.must_equal 0
    end

    describe "when element added" do
      it "length reflects the change" do
        @arr << "a"
        @arr.length.must_equal 1
      end

      describe "when another element added" do
        it "length also reflects the change" do
          @arr << "b"
          p @arr
          @arr.length.must_equal 2
        end
      end
    end
  end
end

describe嵌套在另一个describe - 中,就像我们为RSpec 所做的那样,但不幸的是,@arr << "a"的结果似乎未被保留,嵌套的describe的{​​{1}}也是1。

我已在代码中留下@arr.length,因此您可以在控制台中轻松查看当前存储在p @arr中的内容。

绝对不是我们的期望......让我们尝试一下疯狂然后......

4)。让我们在@arr内嵌套describe

it

嗯,事实证明,这是完全有效的,它的行为完全符合我们的预期! (同样,describe Array do before do @arr = [] end describe "when initialized" do it "is empty" do @arr.length.must_equal 0 end describe "when element added" do it "length reflects the change" do @arr << "a" @arr.length.must_equal 1 describe "when another element added" do it "length also reflects the change" do @arr << "b" p @arr @arr.length.must_equal 2 end end end end end end 离开此处,以便您在“p @arr中检查当前存储的内容。”

老实说 - 我没有用@arr检查,但这个简单的例子很有希望。请尝试相应地修改代码,因此在您的规范中,Capybara互动已实施。

让我明确提供解决方案:我强烈建议再次这种方法:

  • 这样的规格难以阅读 - 结果 - 很难维护。
  • 这被认为是糟糕的模式。 下一步步骤不应依赖之前步骤的结果。

如果你想正确测试你的控制器,你应该尝试类似的东西(这是伪代码只是为了说明想法):

测试第1步

Capybara

测试第2步

post "/url-to-my-form", { params_step_1: { ... } }

使用这种方法很容易看出post "/url-to-my-form", { params_step_1: { ... }, params_step_2: { ... } } params是什么,因此更容易测试,例如。违反任何规则(空值,无效的电子邮件等......)。

希望有所帮助!祝你好运!