如何在页面顶部包含javascript_include_tag的javascript以进行水豚测试

我试图用capybara在spec测试中测试一个javascript类。 我有静态页面player.haml,我在其中使用javascript_include_tag加载javascript类定义。 它看起来像那样

.container = javascript_include_tag "../player" #player 

我的测试看起来像那样

  it 'can autoplay a video' do visit player_path video = Fabricate(:video) options = { autoplay: true, ... } page.execute_script("var options = '#{options}'; videoId = '#{video.id}'; var player = new Player(videoId, options);") expect(page).to have_content(video.title) end 

问题是javascript_include_tag包含页面底部的脚本和execute_script调用之后的脚本。 我在访问该页面后尝试了睡眠,但结果是一样的。 如何在使用execute_script之前加载类定义? 最终的瓶颈是具有播放器定义的文件是.coffee.erb。 我可以更改,因为它使用应用程序中的一些设置。