How to use ActiveRecord without Rails
This is an example of how to use ActiveRecord without Rails:
1 ['/model', '/db'].each do |folder| 2 $:.unshift File.dirname(__FILE__) + folder 3 end 4 5 require 'test/unit' 6 require 'rubygems' 7 require 'activerecord' 8 9 ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log') 10 ActiveRecord::Base.configurations = YAML::load(IO.read(File.dirname(__FILE__) + '/config/database.yml')) 11 ActiveRecord::Base.establish_connection('sqlite3') 12 13 require 'schema' 14
Schema contains, for example:
1 ActiveRecord::Schema.define :version => 0 do 2 create_table :languages, :force => true do |t| 3 t.string :name 4 end 5 end
How to use RSpec and ZenTest in a standalone Ruby project
First install the RSpec and ZenTest gem:
1 $ sudo gem install rspec zentest
Next create the spec folder:
1 $ cd project_folder 2 $ mkdir spec
Save the following to spec/helper.rb:
1 $LOAD_PATH.unshift File.dirname(__FILE__) + '/..' 2 3 require 'rubygems' 4 require 'spec' 5 #require 'spec/rake/spectask' not needed, because ZenTest supports rspec now
Now create spec/transcoder_spec.rb, and add the following test to it:
1 require File.dirname(__FILE__) + '/helper' 2 require File.dirname(__FILE__) + '/../transcoder.rb' 3 4 context "Transcoder" do 5 setup do 6 # Setup your stuff here 7 end 8 9 it "should support 3gp format" do 10 Transcoder.convert("me_and_you.3gp").should == true 11 end 12 end
Let’s not forget the class we’re testing, put this code in lib/transcoder.rb:
1 class Transcoder 2 def initialize 3 end 4 5 def self.convert(file) 6 return true 7 end 8 end
Note that autotest automatically looks for your code in the lib folder.
Now run the test with the zentest command:
1 autotest
Change your files and autotest will run the test again.
Tip: Read Getting started with Autotest – Continuous Testing and Setting up autotest to use Growl on OSX for more information on how to increase your productivity.