gpt4 book ai didi

ruby-on-rails - Rspec 测试需要 sleep 才能工作

转载 作者:行者123 更新时间:2023-12-04 15:58:15 25 4
gpt4 key购买 nike

当我单独运行我的“网络研讨会”规范时,它们似乎总是过去,但如果我尝试整个套件,它只能通过大约 50% 或时间的测试之一。我每次都使用相同的种子对此进行测试,以查看它是否与执行测试的顺序有关。

如果我通过在它中间放置一个 sleep 来减慢我的测试,那么它会神奇地再次开始通过 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

我很抱歉有这么多与这个问题相关的代码。我只是想提供尽可能多的信息,因为我不知道这个错误来自哪里或如何修复它

最佳答案

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

关于ruby-on-rails - Rspec 测试需要 sleep 才能工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21318388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com