如何在 Cucumber 中测试不同的环境(例如开发|测试|生产)? [英] How can I test different environments (e.g. development|test|production) in Cucumber?
问题描述
以这个场景为例.我有一个 Google Analytics 跟踪代码,我只希望它以生产模式显示.所以我可能有两个这样的场景:
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
所以虽然我知道如何检查当前环境是什么,并且知道如何在 Rails 或 Sinatra 中设置当前环境,但我不知道如何在特定环境中运行特定场景.有没有可能?
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?
推荐答案
您应该能够在测试代码本身中强制环境,类似于ENV['RACK_ENV'] = '测试'ENV['RACK_ENV'] = '生产'
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.
我之前不得不搞乱环境(http://richardconroy.blogspot.com/2010/01/issues-testing-sinatra-datamapper-app.html),以获取测试代码以识别它应该针对测试环境执行.我想这只是倒退.
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.
不过,谷歌分析跟踪 cookie 不是特定于网站的吗?在您的开发/测试/暂存环境中使用跟踪 cookie 不会影响您的统计数据.
Still, aren't google analytics tracking cookies site specific? Having the tracking cookie in your development/test/staging environment shouldn't influence your stats.
这篇关于如何在 Cucumber 中测试不同的环境(例如开发|测试|生产)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!