【问题标题】:Can I use before :all with capybara?我可以在 :all with capybara 之前使用吗?
【发布时间】:2012-03-20 12:37:23
【问题描述】:

我有一个这样的描述块:

describe "Documents" do
  subject { page }
  let (:course) { FactoryGirl.create(:course) }
  describe "new" do
    before do
      visit new_course_document_path(course)
      fill_in "Name", with: "TestDocument"
      attach_file "Original document", "#{Rails.root}/spec/fixtures/03_GUI_concurrency.pdf"
    end
    it { should have_selector('title', text:"Upload document")}
    it { should have_selector('h1', text:"Upload document")}
    describe "when clicking upload" do
      before { click_button "Upload Document" }
      it "should have the document name" do
        subject.should have_selector('p', text: "TestDocument")
      end

      it "should have 22 pages" do
        subject.should have_selector('.page', count: 22)
      end

      describe "when visiting the course page" do
        before { visit course_path(course) }
        it { should have_selector 'li', text: "TestDocument"}
      end
    end
  end

测试非常昂贵,因为保存文档需要做大量工作。这很好,但它甚至更慢,因为它实际上是上传 3 次。因此,显而易见的事情是将 before 块变成 before :all 块 - 但是当我这样做时,只有第一个 it {} 块被正确执行,之后的块在空页面上执行,所以它们失败了。

之前:所有块都应该与水豚一起使用,如果是这样,我在这里做错了什么?

【问题讨论】:

    标签: ruby-on-rails ruby rspec capybara


    【解决方案1】:

    Capybara resets session 在每个示例运行之后,因此当您将 visit new_course_document_path(course) 移动到 before (:all) 块中时,从第二个示例开始您将没有什么可访问的。

    我建议只运行您正在处理的测试。这可以通过 RSpec tag optionguard-rspec 来实现,它会为您节省大量时间。

    【讨论】:

    • 是的,我已将这些慢速测试放在他们自己的文件中,并由 CI 运行。不过,这似乎很浪费,似乎这可以使规范运行得更快,用于使用 capybara 进行的大量测试......
    猜你喜欢
    • 1970-01-01
    • 2015-11-10
    • 2016-10-05
    • 2011-07-19
    • 2016-05-09
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 2016-01-20
    相关资源
    最近更新 更多