Testing your Rails Plugins April 24th, 2008

A great feature in Ruby is the integrated testing. Ruby on Rails also implements and extends the Ruby testing framework, and if you’re not testing your Ruby code you should start doing it now!

Normally, when I write tests for a class, I mock out everything not related directly to the class (I do this with mocha). For example, when writing tests for the Mollom gem, I mocked out the whole XMLRPC API, because I can’t rely on them to give me all test cases.

# from http://github.com/DefV/ruby-mollom/tree/master/test/mollom_test.rb
def test_server_list
  xml_rpc = mock
  xml_rpc.expects(:call).
          with('mollom.getServerList', is_a(Hash)).
          returns(['http://172.16.0.1', 'http://172.16.0.2', 'https://172.16.0.2'])

  XMLRPC::Client.stubs(:new).with('xmlrpc.mollom.com', '/1.0').returns(xml_rpc)

  assert_equal [
          {:ip => '172.16.0.1', :proto => 'http'}, 
          {:ip => '172.16.0.2', :proto => 'http'}, 
          {:ip => '172.16.0.2', :proto => 'https'}
         ], @mollom.server_list
end

This way, I’m only testing the behaviour of that specific class, so if something fails somewhere, I can pinpoint the problem, while being sure it’s not a problem in the Mollom API.

While writing a test for some view plugin the other day, I was doing the same thing. I stubbed out everything from ActionView/ActionController which I used (content_tag and url_for and such). Suddenly I realised that wasn’t a good idea.

Rails plugins tests should use the Rails Framework, because the plugins themselves rely on it deeply. To be sure the plugin keeps working while the framework changes, I just have to run my tests, which is exactly what I want.

So I do it this way now:

# from http://github.com/DefV/title_helper/tree/master/test/title_helper_test.rb
require 'test/unit'
require File.dirname(__FILE__) + '/../../../../config/boot.rb'
require File.dirname(__FILE__) + '/../../../../config/environment.rb'

class TitleHelperTest < Test::Unit::TestCase
  def setup
    @helper = ActionView::Base.new
  end

  # Replace this with your real tests.
  def test_title_method_with_no_title_set
    assert_equal "foobar", @helper.title(:site_name => 'foobar')
  end
  ...
end

How do you test your Rails Plugins?

tags: l 0 comments »

Symbols vs Strings April 22nd, 2008

When to use Symbols and when to use Strings in Ruby is really a personal preference. Most of the time I program without taking the difference in performance in consideration.

I just use a few simple rules

in a hash it’s always :key => ‘value’

redirect_to :controller => 'pages', :action => 'index'

special keywords are symbols

Page.find(:all)
Page.find(:first)
redirect_to(:back)

attributes and status’es are symbols

update_attribute(:name, 'Jan')
comment.state = :approved

Except for those rules it’s really what feels right while coding it.

tags: l 1 comments »

HWYDI: String Hex to Decimals April 22nd, 2008

How would You do it?!

I want to make a returning item out of these kinds of posts. I present a problem I had, and the way I solved it, and then you can tell how you did it (In the comments, in your own blog, ..).

I think this can be really interesting, since everyone has their own solution to problems, and nobody knows every possible way to solve something.

the Problem

At Openminds we generate a Virtual Server’s MAC Address by the IP the Virtual Server will get. So for example, host 88.151.243.8 will have MAC address 02:00:58:97:f3:08, where the last 4 parts are the hexadecimal representation of the IP address.

Calculating the IP to the MAC address is easy, but sometimes we want to know what IP address is linked to a certain mac. Most of the time we’ll get our mac addresses in the form of 0200.5897.f308, so we take this as our default input.

my Solution

#!/usr/bin/ruby
mac = ARGV[0] # => "0200.5897.f308"

if mac =~ /(\w{4})\.(\w{4})\.(\w{4})/
  ip = ($1 + $2 + $3).scan(/../)[-4..-1].collect do |hex|
    sprintf('%d', "0x#{hex}")
  end.join('.')
end

puts ip

How would You do it?

tags: l 2 comments »

All things Ruby April 21st, 2008

I used to have 1 blog where I talked about everything I cared about, which was a mix of personal messages (“I’ve seen a movie” and “I’ve bought Guitar Hero III”) and technical messages (Rails Plugins, Akismet Bugs, …). I also mixed English and Dutch in those articles.

It came to a point where my friends and family (not-techies) started complaining that I did too much technical blogging, whilst the people who read my blog for the technical posts got annoyed by my personal blabbering.

No more! From now on all my technical blogging will move here. All in English, nothing dumbed down. My personal life and everything around it will stay on my original blog. That way I hope to keep my “fanbase” happy.

It’s a hard job to maintain 1 blog with useful posts.. Let’s see what 2 blogs give ;-)

tags: l 1 comments »