Test functionality in isolation with Autospec

Ruby posted about 1 year ago by christian

This configuration will make autospec run “spec/models/xyz_spec” when a file in “lib/xyz/ is modified”. No other tests are run.

Put the following code in ./.autotest:

   1  Autotest.add_hook :initialize do |at|
   2    at.clear_mappings
   3    %w{.svn .hg .git vendor}.each {|exception| at.add_exception(exception)}
   4  
   5    at.add_mapping(%r%^lib/xyz/.*\.rb$%) {
   6      at.files_matching %r%^spec/models/xyz_spec\.rb$%
   7  #
   8  # Uncomment if more tests are needed...
   9  # +   at.files_matching %r%^spec/models/xyz_spec\.rb$%
  10  #
  11    }
  12  end

Tagged autotest, testing, bdd, tdd, autospec, zentest

How to use RSpec and ZenTest in a standalone Ruby project

Ruby posted over 2 years ago by christian

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.

Tagged rspec, autotest, zentest, standalone, ruby