Rspec测试需要睡眠才能工作

Bri*_*n H 6 sleep rspec ruby-on-rails 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
Run Code Online (Sandbox Code Playgroud)

.

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
Run Code Online (Sandbox Code Playgroud)

.

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
Run Code Online (Sandbox Code Playgroud)

.

<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>
Run Code Online (Sandbox Code Playgroud)

.

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
Run Code Online (Sandbox Code Playgroud)

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

Bri*_*n H 1

问题似乎最终是 JavaScript 淡入。我们尝试按下的删除按钮位于淡入模式中,以提醒您删除的后果并要求您确认。我们的 wait_for_ajax() 助手等待所有活动的 jQuery 连接被解析。连接将完成,因此它将继续执行下一行代码,告诉它单击删除链接上的链接。html 中有一个删除链接,因此水豚可以找到它,但由于它正在主动淡入……单击不起作用,测试失败!

  • 我们遇到了类似的问题,并通过添加 `= javascript_tag '$.fx.off = true;' 来避免它 if Rails.env.test?` 进入我们的 `layout/application.html.haml` 这将关闭所有测试动画 (3认同)