Translation(s): none

ruby tests

Introduction to software testing

Most of the ruby libraries contain test code which uses the functionality provided by the library and compare results with known inputs. Commonly used testing framework is rspec. Look in your gem to see if there is a 'spec' directory. Other test frameworks may use a 'test' directory.

Running tests

If the gem has tests defined (see if it has 'test or 'spec' directories), make sure you run them successfully. By default gem2deb creates 'debian/ruby-tests.yaml', but it may not work always. If your test or spec directory contains files with extentions other than .rb, this is most certainly going to fail. You can try removing non-ruby files from ruby-test-files.yaml and rebuild it. If it still does not work, try the other two methods described below.

Note: If you don't find ruby-tests.rb in debian directory, you have to create it from the sample file given above. You have to remove ruby-test-files.yaml too.

Uncomment this line in ruby-tests.rb to add custom library paths.

$: << 'lib' << '.'

If it could be done with not much effort, consider instead patching the tests not to override default $LOADPATH (aka $:) variable.


1. Gem provide a test runner (ruby-vegas) - It has test_vegas_runner.rb, so just use it from ruby-tests.rb

 require 'test/test_vegas_runner.rb'

Dealing with Bundler usage

Tips for rails testapp/dummy app or when you can't easily remove bundler usage by patching. Some libraries meant to work with rails create a dummy rails app to test their functionality with a rails app. "case six: bundle install" tells you how to avoid bundler completely. If that fails, try the steps below.

  1. Remove Gemfile.lock (use quilt) as we can't expect debian environment to match exact versions of libraries used by the upstream developers.

  2. Add build dependencies on bundler and rubygems-integration
  3. Patch out Gemfile/gemspec/metadata.yml if bundler insist on a specific version of library and debian already has a newer version. Relax the dependency as ">="

fixing errors

Tips to fixing test errors:

Case one: a required library is missing

no such file to load -- shoulda (LoadError), 

search for shoulda ie,

$ apt-cache search shoulda

If you find it is already packaged, install it with

# apt-get install ruby-shoulda

Note: Remember to add the build dependency you just found to Build-Depends: in debian/control (edit this file)


Build-Depends: debhelper (>= 7.0.50~), gem2deb (>= 0.3.0~)


Build-Depends: debhelper (>= 7.0.50~), gem2deb (>= 0.3.0~), ruby-shoulda

Note: If you see something like "no such file to load -- rack/test (?LoadError)", look for "rack-test" as the gem name.

gem list -r shoulda

If you find it in the remote gem list, then you need to package it first :) So start from step 1 for this gem and come back to your current gem once your package enters debian!

Case two: lib or testor spec is not in library load path

  in `require': no such file to load -- test_helper (LoadError)

$: << 'lib' << '.' 


$: << 'test'  << '.' 

This adds test directory to load path and your tests will be able to find test_helper.

Case three: Argument Error

Example error statement : 'ensure_shared_example_group_name_not_taken'

This happens due to some namespacing error. Refer to this stackoverflow thread for more details.

Change this:

    Dir['{spec}/**/*.rb'].each { |f| require f }


    Dir['{spec}/**/*_spec.rb'].each { |f| require f }

Case four: missing test folder from rubygems org

Error: gem2deb couldn't create a spec or test folder, as it is missing in the gem. At upstream, usually github, it is existent.

Solution: create a directory with the ruby-gem name and make it an empty git repo

mkdir ruby-subexec
cd !$
git init

Download the upstream tarball from github. Add tags to the homepage URL, so you will get a tar.gz file: You can download it like usual from there (or wget if you prefer)

import the upstream version to git with

git-import-orig --pristine-tar ~/Downloads/subexec-0.2.2.tar.gz 

You can now copy the complete debian directory gem2deb created for you and then go on like usual:

cp -a path/to/gem2deb_build/ruby-subexec/debian path/to/actual_build/ruby-subexec 

Note: Request upstream to include tests in future version of the gem itself. See for an example.

Case five: Tests failure due to interactions

Sometimes, The tests may pass perfectly when they are run separately and fail when we are building it because then the tests are running together and they may interact with each other. To see if the tests run okay individually run the following commands inside the test directory.

RUBY=ruby1.8 ./
RUBY=ruby1.9.1 ./

If the tests are passing now, you may want to figure out a way and edit the debian/ruby-tests.rb file by which the tests can run without interacting with each other. One such fix made to the ruby-tests.rb file is as follows.

Dir['test/*_test.rb'].each do |f|
  puts f        
  system("#{ENV['RUBY_TEST_BIN']} -Idebian/ruby-timecop/usr/lib/ruby/vendor_ruby:./test  #{f}") or raise        

The new ruby-tests.rb has a loop: for every test file, call ruby with this test file, and raise an exception if the test fails this way, all the tests are run separately.

Case six: bundle install

If you get an error like this asking you to run bundle install for any gem,

Could not find gem 'rake (>= 0) ruby' in the gems available on this machine.
Run `bundle install` to install missing gems.

Then it means the package is using bundler to satisfy dependencies. Building a deb package should not require use of bundler to install dependencies, but instead all dependencies should be provided in debian/control file as "Build-Depends:" and "Depends:"

You'll have to create a patch to un-comment use of bundler or rubygems like this. (example from ruby-rack-rewrite). Use quilt to create the patch. See basic quilt for help in using quilt.

--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -1,13 +1,18 @@
-require 'rubygems'
-require 'bundler/setup'
+# We'll add dependencies in debian/control
+#require 'rubygems'
+#require 'bundler/setup'

-Bundler.require :default, :development
+#Bundler.require :default, :development

 require 'test/unit'                               

See "Case seven" below for a solution to errors caused by this.

Case seven: NameError

If you get an error similar to this,

./test/rack-rewrite_test.rb:47: undefined method `context' for RackRewriteTest:Class (NoMethodError)

It usually means the library is using bundler to require other libraries and which are missing, see "Case six" above for details. We can add those dependencies manually by patching test_helper.rb or spec_helper.rb as is the case.

--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -1,13 +1,18 @@

 require 'test/unit'
+require 'shoulda'
+require 'mocha'
+require 'rack'
+require 'rack/rewrite'

 class Test::Unit::TestCase

 def supported_status_codes
   [:r301, :r302, :r303, :r307]
\ No newline at end of file

In the above example, the dependency is found from Gemfile, and corresponding gemspec

source ""


group :development do
  gem 'rake'

Dependency section of rack-rewrite.gemspec is given below

  s.add_development_dependency 'bundler'
  s.add_development_dependency 'shoulda', '~> 2.10.2'
  s.add_development_dependency 'mocha', '~> 0.9.7'
  s.add_development_dependency 'rack'

Using these as reference, we add those as require lines in the patch. If you cannot solve case six and seven, deal with bundler and follow Dealing with Bundler usage"