4

Take this scenario. I have a Google Analytics tracking code, and I only want it to show up in Production mode. So I might have two scenarios like this:

Scenario: Don't embed tracking code in development or test mode
  Given the app is not in production mode
  When I go home
  Then I should really not see the tracking code

Scenario: Embed tracking code in production mode
  Given the app is in production mode
  When I go home
  Then I should really see the tracking code

So although I know how to check what the current environment is, and I know how to set the current environment in Rails or Sinatra, I have no idea how to run a specific scenario in a specific environment. Is it even possible?

Brandon Weiss
  • 591
  • 1
  • 5
  • 14
  • Why would you want to test different environments? – Joshua Partogi Jul 30 '10 at 04:50
  • Hmm, maybe I did a bad job of explaining it. I don't want to embed the analytics tracking code unless it's in a production environment. So in the view I would check to see what the environment is, and if it's production, embed the code. But let's say I wanted to test it. How would I do that? – Brandon Weiss Jul 30 '10 at 05:34
  • This is a pretty trivial thing - `if Rails.env.production? ... do the tracking code`, and is the kind of thing I wouldn't personally bother testing. – Robert Speicher Jul 30 '10 at 08:55
  • Yes, I know how to implement it; the problem is TESTING the implementation. Tests run in their own environment; the `test` environment. The questions is, how can I run a specific scenario in an alternate environment? – Brandon Weiss Jul 31 '10 at 00:43
  • I understood perfectly your question, I just don't understand why you're asking it. It's a one-line conditional statement that always works because it's using Rails, which is well-tested. You could just start your server in production once, see that your tracking code is there, and call it good. – Robert Speicher Jul 31 '10 at 17:03
  • Ah, I see what you're saying. That's my fault, I should have explained better; I was just using this scenario as a theoretical example. I certainly agree with you that for something simple like this it wouldn't make sense to test it. But my end goal was to find out that if you had some more complex logic that was based on different environments, would it actually be possible to write tests for it, or is it just not possible to run a particular test in a different environment (irregardless of whether or not it would make sense to do so)? – Brandon Weiss Aug 03 '10 at 19:20

2 Answers2

3

I think you should really hit the live URL of your website using cucumber; because that's what you probably really want to know-- is my tracking running, like for real?

This worked for me, but you'll need to use Capybara (perhaps someone will post a similar webrat solution if it exists).

Given /^the app is in production mode$/ do
  Capybara.current_driver = :selenium
  Capybara.app_host = 'http://www.joshcrews.com'
end

When /^I go home$/ do
  visit("http://www.joshcrews.com")
end

Then /^I should really see the tracking code$/ do
  page.body.should match /UA-7396376-1/
end
Josh Crews
  • 779
  • 2
  • 11
  • 21
1

You should be able to force the environment in the test code itself, something along the lines of ENV['RACK_ENV'] = 'test' ENV['RACK_ENV'] = 'production'

I would consider it a pretty bad code smell though.

I have had to mess about with environments before(http://richardconroy.blogspot.com/2010/01/issues-testing-sinatra-datamapper-app.html), to get test code to recognise that it should be executing against test environments. This is just doing it backwards I suppose.

Still, aren't google analytics tracking cookies site specific? Having the tracking cookie in your development/test/staging environment shouldn't influence your stats.

Richard Conroy
  • 1,880
  • 1
  • 12
  • 4
  • Ah, I hadn't thought of just re-setting the environment variable. Yes, it doesn't matter if analytics is in your dev or test environments, however, if you're using the non-asynchronous snippet and you don't have an internet connection, then your page will timeout, I think. But that's all moot anyways, it was just a theoretical question about running tests that check for an environment. Thanks! – Brandon Weiss Aug 09 '10 at 04:28