Can I use before :all with capybara?

2020-03-02 07:04发布

I have a describe block like this:

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

The test is quite expensive since significant work is done on saving the document. That's fine, but it's even slower since it's actually doing that upload 3 times. So, the obvious thing to do is to make the before blocks into before :all blocks - but when I do that, only the first it {} block is executed correctly and the ones afterward are executing on an empty page so they fail.

Are before :all blocks supposed to work with capybara, and if so what am I doing wrong here?

1条回答
神经病院院长
2楼-- · 2020-03-02 07:34

Capybara resets session after each example run, so when you moved visit new_course_document_path(course) into before (:all) block, you had nothing to visit starting from a second example.

I would recommend to run only that tests you're working on. This could be achieved with a RSpec tag option or guard-rspec, and it'll save you a LOT of time.

查看更多
登录 后发表回答