Padrino test_config import

In Padrino when you want to create a test file, you should use the test_config.rb in /tests directory. If you create a model by generator, your code looks like this:

1
2
3
4
5
6
7
8
require File.expand_path(File.dirname(__FILE__) + '/../test_config.rb')
 
describe "User Model" do
  should "construct a new instance" do
    @user = User.new
    refute_nil @user
  end
end
require File.expand_path(File.dirname(__FILE__) + '/../test_config.rb')

describe "User Model" do
  should "construct a new instance" do
    @user = User.new
    refute_nil @user
  end
end

I usually play TDD, so when I need a model I just create the test file and step by step adding the additional files which are required by the test. The first line of the previous snippet is just pain in my ass! I don’t want to write this in my code, I just want to use it like in Rails:

1
2
3
4
5
6
7
8
require "test_config.rb"
 
describe "User Model" do
  should "construct a new instance" do
    @user = User.new
    refute_nil @user
  end
end
require "test_config.rb"

describe "User Model" do
  should "construct a new instance" do
    @user = User.new
    refute_nil @user
  end
end

The solution for this problem is very easy, just open /tests/test.rake file, and extend the configuration with test.libs:

1
2
3
4
5
6
7
test_tasks.each do |folder|
  Rake::TestTask.new("test:#{folder}") do |test|
    test.pattern = "test/#{folder}/**/*_test.rb"
    test.verbose = false
    test.libs << File.expand_path(File.dirname(__FILE__))  # Here is the trick
  end
end
test_tasks.each do |folder|
  Rake::TestTask.new("test:#{folder}") do |test|
    test.pattern = "test/#{folder}/**/*_test.rb"
    test.verbose = false
    test.libs << File.expand_path(File.dirname(__FILE__))  # Here is the trick
  end
end

I don’t understand Padrino developers why they don’t use this option, but feel free to turn it on!