简体   繁体   中英

Can I use before :all with capybara?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM