6

I have some common methods used in a couple different specs, I want to extract them to some place like a spec helper that is accessible from all specs. Anyone know how to do this?

jacob
  • 2,284
  • 3
  • 20
  • 21

3 Answers3

1

Here is something that sorta quacks like a 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

And then use it:

# my_view_spec.rb

describe "MyView" do
  extend SpecHelper

  before do
    foo_helper
  end
  ...


Two things to bear in mind:

  1. Spec helper file is named in such way that it gets loaded first (leading underscore)

  2. When running individual specs (e.g. files=my_view_spec.rb) helper file must go along - files=spec/my_view_spec.rb,spec/_spec_helper.rb

artemave
  • 6,786
  • 7
  • 47
  • 71
  • I used this solution to solve a slightly different problem, so thanks very much for this! I hope something "official" like this eventually finds its way into Rubymotion. – Paul Fioravanti Jun 05 '13 at 08:24
0

I just throw my common methods used in specs as they are (not encapsulated in a Module or anything) in a spec/support/utilities.rb file and Rubymotion seems to pick them up fine, though I don't know if this is the "proper" way to do this.

Paul Fioravanti
  • 16,423
  • 7
  • 71
  • 122
0

According to current http://www.rubymotion.com/developer-center/articles/testing/#_spec_helpers

Spec helpers are created under the spec/helpers directory of a RubyMotion project. An example could be spec/helpers/extension.rb.

Justin Love
  • 4,397
  • 25
  • 36