Register now and start sharing your code snippets.

Dynamic RSpec tests using plain old Ruby

Ruby posted 6 months ago by christian

Just happened to stumble upon the following article at caboo.se: Handy dynamic rspec tip.

I immediately found a way of simplifying a test case that involves testing that an ever increasing number of videos can be transcoded:

   1  require File.dirname(__FILE__) + '/helper'
   2  require File.dirname(__FILE__) + '/../lib/transcoder.rb'
   3  
   4  context "Transcoder" do
   5    
   6    Dir.glob('videos/*').each do |video|
   7      it "should support #{video}" do
   8        Transcoder.convert(video, "site/#{video}.flv")[:file_size].should > 0
   9      end
  10    end
  11  end

You can also use it to test model validations:

   1  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
   2  
   3  
   4  describe "Product" do
   5    @@valid_product_attributes = { 
   6        :name         => 'WTF',
   7        :description  => 'LOL',
   8        :price        => '10.0',
   9        :tax          => '22.0'  }
  10  
  11    
  12    before(:each) do
  13  
  14    end
  15  
  16    it "should create a new instance given valid attributes" do
  17      Product.create!(@@valid_product_attributes)
  18    end
  19    
  20    @@valid_product_attributes.each do |name, value|
  21      it "should not allow blank #{name}" do
  22        lambda do
  23          Product.create!(@@valid_product_attributes.except(name))
  24        end.should raise_error(ActiveRecord::RecordInvalid)
  25      end
  26    end
  27  end

Tagged rspec, ruby, validation, trick, tip

How to use RSpec and ZenTest in a standalone Ruby project

Ruby posted 6 months 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