我可以在 :all 与水豚一起使用之前使用吗? [英] Can I use before :all with capybara?

查看:22
本文介绍了我可以在 :all 与水豚一起使用之前使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的描述块:

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

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

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 在每个示例运行后重置会话,所以当你将 visit new_course_document_path(course) 移动到 before (:all) 块时,你没有什么可访问的从第二个例子开始.

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.

我建议只运行您正在处理的测试.这可以通过 RSpec 标签选项guard-rspec,它会为你节省很多时间.

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.

这篇关于我可以在 :all 与水豚一起使用之前使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆