Rspec测试需要睡眠才能工作

时间:2014-01-23 20:14:29

标签: ruby-on-rails rspec sleep timing

当我单独运行'网络研讨会'规格时,它们似乎总是过去,但如果我尝试整个套件,它只会通过其中一项测试,大约50%或者时间。我每次使用相同的种子测试它,看看它是否与执行测试的顺序有关。

如果我通过在它的中间睡一觉来减慢我的测试,那么它再次神奇地开始100%通过。显然,我不想依赖这样的弱工作,想要弄清楚如何真正解决我的问题。 需要“spec_helper”

require "spec_helper"

describe "ProgramManager::Webinars" do
  let(:program) { create(:program) }
  let(:superuser) { create(:superuser) }

  describe "#index" do
    before { login_as(superuser) }
    let(:delete) { 'Delete' }

    it "displays an edit and destroy link for all webinars" do
      w1, w2, w3 = create(:webinar, program: program), create(:webinar, program: program), create(:webinar, program: program)
      visit program_webinars_path(program)
      [w1, w2, w3].each do |webinar|
        expect(page).to have_link webinar.name, href: edit_program_webinar_path(program, webinar)
        expect(page).to have_link '', href: destroy_warnings_program_webinar_path(program, webinar)
      end
    end

    it "has a link to create a new webinar" do
      visit program_webinars_path(program)
      expect(page).to have_content 'New Webinar'
    end

    it "deletes a webinar", js: true do  #THIS IS THE TEST THAT SOMETIMES FAILS
      webinar = create(:webinar, program: program)
      visit program_webinars_path(program)
      all('.destroy').last.click
      wait_for_ajax
      sleep(1.second)         #THIS IS THE SLEEP THAT FIXES THE FAILURE
      expect { click_link delete }.to change(Webinar, :count).by(-1)
    end
  end

FactoryGirl.define do
  factory :webinar do
    program
    name "some name"
    url "some url"
    description "some description"
    speaker "some speaker"
    starts_at Time.now
  end
end

FactoryGirl.define do
  factory :program do
    contract
    program_manager factory: :user
    sequence(:name) { |n| "Program-#{n}" }
    description     { "Program description" }
    starts_at       { Time.now }
    ends_at         { Time.now + 10.days }
    color           { "#33f" }
    program_type    { "some program type" }
    verified        { false }
  end
end

<div class="col-md-4">
  <%= link_to "<span class='glyphicon glyphicon-plus'></span>".html_safe, new_program_webinar_path(@program), class: 'new-webinar', data: { toggle: 'tooltip', title: 'Add a Webinar' } %>
  <h4>Current Webinars</h4>

  <% if @webinars.empty? %>
    <p>There are currently no webinars to display.</p>
  <% else %>
    <table class="table table-condensed">
      <% @webinars.each do |webinar| %>
        <tr>
          <%= content_tag :td, class: pm_setup_classes(webinar, @webinar) do %>
            <%= link_to "<span class='glyphicon glyphicon-remove'></span>".html_safe, destroy_warnings_program_webinar_path(@program, webinar), class: 'destroy', data: { toggle: 'modal', target: '#ajax-modal' } %>
            <%= link_to webinar.name, edit_program_webinar_path(@program, webinar), class: 'webinar' %>
          <% end %>
        </tr>
      <% end %>
    </table>
  <% end %>

  <%= link_to 'New Webinar', new_program_webinar_path(@program), class: 'btn btn-success btn-block' %>
</div>

class ProgramManager::WebinarsController < ProgramManager::ApplicationController
  before_filter :authenticate_user!
  before_filter :webinars

  def new
    @webinar = @webinars.build
    clean_webinars
  end

  def create
    @webinar = @program.webinars.build(params[:webinar])
    clean_webinars
    if @webinar.save
      redirect_to program_webinars_path(@program), success: "Webinar successfully created"
    else
      render :new
    end
  end

  def edit
    @webinar = @program.webinars.find(params[:id])
  end

  def update
    @webinar = @program.webinars.find(params[:id])
    if @webinar.update(params[:webinar])
      redirect_to program_webinars_path(@program), success: "Webinar successfully updated"
    else
      render :edit
    end
  end

  def destroy
    @webinar = @program.webinars.find(params[:id])

    if @webinar.destroy
      redirect_to program_webinars_path(@program), success: "Webinar removed successfully"
    else
      render :index
    end
  end

  def destroy_warnings
    @webinar = @program.webinars.find(params[:id])
    render layout: false
  end

private

  def clean_webinars
    @webinars = @webinars.delete_if(&:new_record?)
  end

  def webinars
    @webinars = @program.webinars
  end
end

很抱歉,这个问题代码太多了。我只是想提供尽可能多的信息,因为我不知道这个bug来自何处或如何解决它

4 个答案:

答案 0 :(得分:1)

问题似乎最终是一个javascript淡入。我们试图按下的删除按钮是一个淡入的模式,提醒你删除的后果,并要求你确认。我们的wait_for_ajax()助手等待,直到所有活动的jQuery连接都得到解决。连接将完成,因此它将转到下一行代码,告诉它单击删除链接上的链接。 html中有一个删除链接,因此Capybara可以找到它,但由于它正在积极淡入......点击不起作用,测试失败!

答案 1 :(得分:0)

您可以调整Capybara.default_wait_time = 5(默认为2秒)。

请参阅https://github.com/jnicklas/capybara

的“异步JavaScript(Ajax和朋友)”部分中的doc

这将改变Capybara在放弃寻找节点之前等待的总时间,但不应影响它将继续尝试检查的时间间隔。

答案 2 :(得分:0)

它也可能是您数据库中的异步操作。在我们的PostgreSQL配置中设置fsync = on之前,我们的规范中出现了随机故障。我认为这比填充睡眠(#)更好。

答案 3 :(得分:0)

我没有看到错误跟踪,但如果页面上没有完全准备好所有数据,您可以使用特定的gem rspec-wait来等待条件一段时间(默认情况下为3)秒)。因此,例如rspec代码变为以下内容:

  visit program_webinars_path(program)
  wait_for(page).to have_content 'New Webinar'

这允许您等待一段特定的HTML(如果需要)一段时间。