简体   繁体   English

我该如何检查coffeescript中的Rails测试环境?

[英]How do I check for the test environment in coffeescript for rails?

I have some coffeescript code that I want to avoid when running my integration tests and cucumber scenarios. 运行集成测试和黄瓜方案时,我想避免使用某些coffeescript代码。 I'm trying to figure out if there is an easy way to introspect on the runtime environment so that if it's 'test', it will do one thing, else do another thing. 我试图找出是否有一种简单的方法可以对运行时环境进行自省,以便如果它是“测试”,它将做一件事,而另一件事。

I don't want to add ERB parsing to the coffeescript file. 我不想将ERB解析添加到coffeescript文件。 Is there any other way to know if it's running in test environment? 还有其他方法可以知道它是否在测试环境中运行吗?

Here's the snippit. 这是snippit。 It sets up a window alert when you leave the page. 当您离开页面时,它会设置一个窗口警报。 This is messing up my cucumber code, so I want to simply skip it if I'm running under Rails.env == 'test' 这搞砸了我的黄瓜代码,所以如果我在Rails.env =='test'下运行,我想简单地跳过它

setupNavigateAwayProtection: ->
  window.onbeforeunload = =>
    if @promptForChanges()
      message = "You are about to lose your changes"
    message

promptForChanges is a function that checks if the content has changed. promptForChanges是用于检查内容是否已更改的函数。 So I could imagine a version like: 所以我可以想象一个类似的版本:

window.onbeforeunload = =>
  if @promptForChanges() && !@test
    message = "You are about to lose your changes"
  message

but how would I define @test to be true if I'm running tests? 但是如果我正在运行测试,如何将@test定义为true?

Thanks, Dan 谢谢,丹

Your code should not behave differently during testing . 您的代码在测试期间的行为不应有所不同

Instead, why don't you encapsulate this logic in a differente file and don't include it in your testing? 相反,为什么不将这个逻辑封装在一个差异文件中,并且将其包含在测试中呢?

That way, the behaviour of the system will be affected, but your code doesn't have to know about it - your testing configuration does. 这样, 系统的行为将受到影响,但是您的代码不必知道它-测试配置会知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM