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

采取这种情况。 我有一个Google Analytics跟踪代码,我只希望它以生产模式显示。 所以我可能有两个这样的场景:

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中设置当前环境,但我不知道如何在特定环境中运行特定方案。 它甚至可能吗?

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

我会认为这是一个非常糟糕的代码味道。

我之前不得不乱用环境( http://richardconroy.blogspot.com/2010/01/issues-testing-sinatra-datamapper-app.html ),以获得测试代码以识别它应该针对测试执行环境。 我想这只是倒退。

仍然不是谷歌分析跟踪cookie网站具体? 在开发/测试/暂存环境中使用跟踪cookie不应影响您的统计信息。

我认为你应该用黄瓜点击你网站的实时url; 因为这可能是你真正想知道的 – 我的跟踪是否正常运行?

这对我有用,但你需要使用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