Skip to content
Vassil Kovatchev edited this page Aug 24, 2017 · 18 revisions

Sometimes you find yourself writing the same helper method over and over again in every specification.
You can factor out those common methods in their own Ruby modules.

Just create a Ruby module in folder lib/helpers and Slacker will automatically make the methods of this module available to all your specifications.

For example, if we create the following file: lib/helper/my_common_methods.rb:

# lib/helper/my_common_methods.rb
module MyCommonMethods
  # Delete all records from MyTable and load a file into it
  def load_file_into_my_table(csv_file)
    query 'delete from dbo.MyTable;'
    load_csv(csv_file, 'dbo.MyTable')
  end
end

...method load_file_into_my_table will automatically become available to your specification code:

describe 'My fabulous sproc'
  it "gets the job done" do
    load_file_into_my_table('file_xyz.csv')
    ...
  end
end

Be careful when you name your module files. Slacker infers the name of the module by looking at the name of the file.

For example, if your module is called Common, it has to be located in file lib/helpers/common.rb. If the module is called MyModule, it should be located in file lib/helpers/my_module.rb.

You can create as many helper modules as you like. Slacker will load them all and will make their methods available to all your specifications.

Classes, etc.

One more thing.
Slacker automatically adds the project's lib folder to the Ruby path. This means that you can require any file located in that folder.
For example, if you had a Ruby class implemented in lib/my_nifty_class.rb, you could load it in your helper modules or specifications like this:

require 'my_nifty_class'

Next, see SQL Trace Scripts.