views:

56

answers:

2

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?

A: 

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
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
A: 

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
Ah, now that's something I hadn't thought of. Good idea!
Brandon Weiss