如何让Aruba使用不同的ENV [“HOME”]?

时间:2013-01-23 11:17:37

标签: ruby cucumber aruba

来自the docs

  

默认情况下,Aruba将创建一个目录tmp / aruba,用于执行文件操作。

但是,我的应用程序使用ENV["HOME"]来创建和读取文件(~/.foorc),因此我需要Aruba使用假的ENV["HOME"]

我是否需要在某个支持文件中设置它,或者是否有办法告诉Aruba tmp/aruba ENV["HOME"]中的文件?

以下是我正在测试的代码的摘录(显然我正在使用Cucumber / Aruba进行更高级别的测试,但ENV [“HOME”]的使用在这里非常重要。):

def initialize config_path = ""
  if config_path.empty?
    @config_path = File.join ENV["HOME"], ".todotxt.cfg"
  else
    @config_path = config_path
  end

  if file_exists?
    super @config_path
    validate
  end
end

def file_exists?
  File.exists? @config_path
end

#....
  ask_to_create unless @config.file_exists?
#...

规范:

Scenario: todotxt
  Given an empty installation
  When I run `todotxt`
  Then it should pass with:
    """
    Should I create a sample config file? [Y/n]
    """

2 个答案:

答案 0 :(得分:2)

在Aruba本身的实施中,我可以制作非常相似的东西:

文件 features / support / aruba.rb ,由黄瓜自动加载并实现Around钩子:

# Temporarily enforce an isolated, fake, homedir.
around do |scenario, block|
  @__aruba_original_home = ENV["HOME"]
  ENV["HOME"] = File.expand_path(File.join("tmp", "aruba"))
  block.call
  ENV["HOME"] = @__aruba_original_home
end

从现在开始,目录 tmp / aruba 用作$ HOME。

请注意,在aruba中,此临时路径是可配置的,并且上面的代码不考虑这一点。在其他地方配置tmp路径时会中断。

答案 1 :(得分:1)

Aruba提供a step for just that

Given a mocked home directory