在rubymotion中创建一个spec帮助器

时间:2012-06-19 03:20:44

标签: specs rubymotion

我在一些不同的规范中使用了一些常用方法,我想将它们提取到某个地方,比如可以从所有规范访问的规范助手。有谁知道怎么做?

3 个答案:

答案 0 :(得分:1)

这就像spec_helper一样嘎嘎叫。

# _spec_helper.rb

module SpecHelper
  ::App::Persistence = {}

  # global `before :each` ish
  def self.extended(base)
    base.before do
      ::App::Persistence.clear
    end
  end

  def foo_helper
  end
end

然后使用它:

# my_view_spec.rb

describe "MyView" do
  extend SpecHelper

  before do
    foo_helper
  end
  ...


要记住两件事:

  1. Spec helper文件的命名方式是首先加载(引导下划线)

  2. 运行个别规范(例如files=my_view_spec.rb)时,必须使用帮助文件 - files=spec/my_view_spec.rb,spec/_spec_helper.rb

答案 1 :(得分:0)

我只是将我们在规范中使用的常用方法(因为它们没有封装在Module或其他任何内容中)放在 spec / support / utilities.rb 文件中,并且Rubymotion似乎选择了他们很好,但我不知道这是否是“正确”的方式。

答案 2 :(得分:0)

根据目前http://www.rubymotion.com/developer-center/articles/testing/#_spec_helpers

  

在RubyMotion项目的spec / helpers目录下创建Spec帮助程序。一个例子可能是spec / helpers / extension.rb。

相关问题