2016-10-31 49 views
0

我有一个按钮,当它被点击时,它会触发一个ajax帖子,并呈现一个警报。水豚-webkit:不正确执行coffeescript

它工作的很好,但是在运行测试时,coffeescript不能很好地执行。

我的文件

应用程序/视图/警报/ index.html.haml

= link_to icon('plus', 'Show Alert'), new_alert_path, class: 'btn btn-with-icon btn-primary', remote: true 

应用程序/控制器/ alerts_controller.rb

class AlertsController < BaseController 
    def new 
    @alert = Alert.new 
    end 
end 

应用程序/意见/alerts/new.js.coffee

console.log "1" # => This is being executed 
AlertComponent.show "show this message" # Here it breaks I think 
console.log "2" # => It never arrives here 

应用程序/资产/ JavaScript的/ alert_component.js.coffee

class AlertComponent 
    @show: (description) -> 
    console.log "3" # => This is not being executed 
    # => Does some other stuff 

end 

window.AlertComponent = AlertComponent 

我的测试

click_link 'Show Alert' # => This is executed because I added a binging.pry in the controller and it arrives properly 
wait_for_ajax 
p page.driver.console_messages # => Below is what it returns 
expect(page).to have_css '.alert-div' 

结果

测试失败,并且页面。 driver.console_messages返回此结果:

{:line_number=>0, :message=>"ReferenceError: Can't find variable: AlertComponent", :source=>"undefined"}

正如我已经说过的,它在开发和生产中非常有用。问题在于测试。

我的设置

我用的水豚,WebKit的:

  • 水豚(2.9.2)
  • 水豚,WebKit的(1.1.0)

规格/ rails_helper .rb

ENV['RAILS_ENV'] ||= 'test' 
require File.expand_path('../../config/environment', __FILE__) 
# Prevent database truncation if the environment is production 
abort("The Rails environment is running in production mode!") if Rails.env.production? 
require 'spec_helper' 
require 'rspec/rails' 

Capybara.javascript_driver = :webkit 

# more stuff 

有关为什么这不按预期工作的任何想法?

回答

0

在讨论rails资产管道时,开发和测试/生产之间的巨大差异在于,每个JS文件在开发时都是单独服务的,而在测试/生产中它们被连接在一起并作为一个文件服务。这意味着一个文件中的JS错误不会阻止其他文件在开发模式下处理,但它可以处于测试/生产模式。看起来它可能会在创建AlertComponent类之前放弃对连接文件的评估。

在开发模式下运行应用程序时,请检查您的浏览器开发人员控制台并修复正在记录的任何JS错误。

+0

感谢您的评论。但是,没有js错误,并且在生产模式下工作得很完美。 –

+0

@ArielScherman然后,请将它在生产中的作用添加到问题中。无论如何,错误告诉你AlertComponent不存在,这意味着alert_component.js.coffee要么不被资产管道包含,要么在文件加载时没有得到处理。假设你实际上包含了该文件(因为它在dev中工作),这可能是由于某个文件中的JS错误(你说没有),使用capybara-webkit无法处理的ES6方法(它基于一个旧的Qt)或在轨道资产管道中的配置错误。 –

+0

正如你所提到的那样,问题似乎是资产连接。我已将资产调试模式设置为false,并向我显示有关AlertComponent的错误。然而,在生产工作正常的情况下很奇怪。你知道我是否在组件上做错了什么?我有一个“require_tree”。在我的application.js和alertComponent文件是我在上面添加的职位描述.... –