ruby-on-rails - 我可以在 :all with capybara? 之前使用吗

标签 ruby-on-rails ruby rspec capybara

我有一个这样的描述 block :

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 block 变成 before :all block ——但是当我这样做时,只有第一个 it {} block 被正确执行,而后面的 block 在空页上执行,所以它们失败了。

之前 :all block 是否应该与 capybara 一起工作,如果是这样,我在这里做错了什么?

最佳答案

capybara resets session在每个示例运行之后,因此当您将 visit new_course_document_path(course) 移动到 before (:all) block 时,从第二个示例开始您没有任何内容可访问。

我建议只运行您正在处理的测试。这可以通过 RSpec tag option 来实现或 guard-rspec ,它会为您节省很多时间。

关于ruby-on-rails - 我可以在 :all with capybara? 之前使用吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9786899/

相关文章:

ruby - 在 Ruby 中从 Unix 套接字读取/写入

ruby-on-rails - 在 Rails 中使用测试数据库

ruby-on-rails - 安装 bcrypt-ruby (3.1.2) 时出错,Bundler 无法继续

ruby - 通过串口从 Ruby 向 Arduino 写入字节

python - 对于 Project Euler,C++ 似乎比 Python Ruby 慢得多

ruby-on-rails-3 - 如何设置 rspec-rails 以生成 capybara 的特性规范

ruby-on-rails - 如果快速规范通过,如何让守卫只运行慢速规范

ruby-on-rails - 为什么在 Rails 3.2 中是 'active_record.mass_assignment_sanitizer = :strict'?

ruby-on-rails - 载波临时目录设置为uploads/tmp文件夹

ruby-on-rails - 此代码能够从文件系统读取 CSV 文件,但是从 URL 读取呢?