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

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

问题描述

我有一个类似的描述块:

 描述文档以
为主题{页面}
let(:course){FactoryGirl.create(:course)}
描述新建,然后
进行
访问new_course_document_path(course)
fill_in名称,带有: TestDocument
attach_file原始文档,#{Rails.root} /spec/fixtures/03_GUI_concurrency.pdf
结束
它{应该具有_selector('title',文本: 上传文档)}
它{应该具有 _upor('h1',文本:上传文档)}}
描述单击上传时在{click_button上传文档}之前执行

它应具有文档名称做
subject.should have_selector('p',文本: TestDocument)
结束

它应具有22 pages do
subject.should have_selector('。page',count:22)
end

描述在访问课程页面时
在{访问之前库尔se_path(course)}
it {应该具有 _selector'li,文本: TestDocument}
结束
结束
结束

该测试非常昂贵,因为在保存文档方面进行了大量工作。很好,但由于实际上载了3次,因此速度甚至更慢。因此,显而易见的事情是将before块设置为before:all块-但是,当我这样做时,只有第一个{}块可以正确执行,而随后的块将在空页面上执行,因此它们将失败。 / p>

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

解决方案

Capybara 重置会话在每个示例运行之后,因此,当您将访问new_course_document_path(course)之前的(:all)块中时,从第二个示例开始,您什么都没需要访问。



我建议仅运行您正在处理的测试。这可以通过RSpec 标记选项 guard-rspec ,它可以为您节省大量时间


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.

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

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