以前可以使用:全部用水豚吗?

Jor*_*rds 6 ruby rspec ruby-on-rails capybara

我有一个这样的描述块:

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
Run Code Online (Sandbox Code Playgroud)

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

以前:所有的块都应该和水豚一起工作,如果是的话,我在这里做错了什么?

Nas*_*ges 5

Capybara 在每个示例运行后重置会话,因此当您移动visit new_course_document_path(course)before (:all)阻止时,从第二个示例开始,您无需访问任何内容.

我建议只运行你正在进行的测试.这可以通过RSpec 标签选项guard-rspec来实现,它可以为您节省大量时间.

  • 是的,我已将这些慢速测试放在他们自己的文件中,它们由CI运行.尽管如此,它似乎相当浪费,似乎这可能使得使用水豚的许多测试的规格运行得更快...... (2认同)