如何在黄瓜中测试不同的环境(例如开发|测试|生产)?

时间:2023-01-14 15:42:58

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?

因此,尽管我知道如何检查当前的环境,并且知道如何设置Rails或Sinatra的当前环境,但我不知道如何在特定的环境中运行特定的场景。它甚至有可能吗?

2 个解决方案

#1


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'

您应该能够在测试代码本身中强制环境,类似于ENV['RACK_ENV'] = 'test' ENV['RACK_ENV'] = '生产'

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.

我以前也曾遇到过环境问题(http://richardconroy.blogspot.com/2010/01/issues-testing-sinatra-datamapper-app.html),为了获得测试代码以识别它应该针对测试环境执行。我想这只是在倒退。

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

不过,谷歌分析跟踪cookie站点不是很特别吗?在您的开发/测试/登台环境中拥有跟踪cookie不应该影响您的统计数据。

#2


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?

我认为你应该用cucumber来点击你网站的实时URL;因为你可能真的想知道——我的追踪真的在跑吗?

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

这对我来说是可行的,但是您需要使用Capybara(如果有类似的webrat解决方案,也许有人会发布)。

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

#1


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'

您应该能够在测试代码本身中强制环境,类似于ENV['RACK_ENV'] = 'test' ENV['RACK_ENV'] = '生产'

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.

我以前也曾遇到过环境问题(http://richardconroy.blogspot.com/2010/01/issues-testing-sinatra-datamapper-app.html),为了获得测试代码以识别它应该针对测试环境执行。我想这只是在倒退。

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

不过,谷歌分析跟踪cookie站点不是很特别吗?在您的开发/测试/登台环境中拥有跟踪cookie不应该影响您的统计数据。

#2


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?

我认为你应该用cucumber来点击你网站的实时URL;因为你可能真的想知道——我的追踪真的在跑吗?

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

这对我来说是可行的,但是您需要使用Capybara(如果有类似的webrat解决方案,也许有人会发布)。

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