简体   繁体   中英

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. 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. Is there any other way to know if it's running in test environment?

Here's the 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'

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. 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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