Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a spec helper in rubymotion

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?

like image 820
jacob Avatar asked Jun 19 '12 03:06

jacob


1 Answers

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

like image 164
artemave Avatar answered Oct 18 '22 02:10

artemave