Month: October 2007

Mass RSpec testing of required fields

Lazy is the way to go:

  [:user_id, :product_id, :person_id, :status, :purpose, :site_id].each do |field|
    it “should have required #{field}” do
      lambda {
        lead = create_lead(field => nil)
        lead.should have_at_least(1).errors_on(field)
      }.should_not change(Lead,:count)
    end
  end 
 

Standard RSpec tests for models

The create_bank method looks like this:

   def create_bank( options = {})
    Bank.create({
        :person_id     => 1,
        :sort_code     => ‘989898’,
        :name          => ‘Natlays Shanghai’,
        :branch        => ‘Fruit Exchange’,
        :address       => ‘Big Street’,
        :tow          => ‘Tow’,
        :county        => ‘Place’,
        :postcode      => ‘L1 5XX’,
        :account       => ‘123456789’,
      }.merge(options))
  end

RSpec tests look a bit like this 

  it “should be valid” do
    lambda {
      create_bank.should be_valid
    }.should change(Bank,:count).by(1)
  end

  it “should have a sort code” do
    lambda {
     bank = create_bank(:sort_code => nil)
     bank.should have_at_least(1).errors_on(:sort_code)
    }.should_not change(Bank,:count)
  end

  it “should belong to a perso” do
    Bank.reflect_on_association(:person).should_not be_nil
  end

 Testing that destroy is cascaded down to child records (Employer can have many addresses):

  it “should delete addresses” do
    address = Address.new
    employer = create_employer
    employer.addresses « address
    address.should_receive(:destroy).once
    employer.destroy
  end
 

Just running RSpec on one thing while you fix it

This is like an Autotest script but you have to tell it what files to watch, also can be used for anything that waits for when a file changes (assumes the file is called mywatch, the echo sh … line is all one line):

#! /bin/sh
if [ “$#” -eq 0 ]
then echo Usage: $0 command files
  echo runs the command every time the files have changed
  echo put the command into quotes
  echo example:
  echo sh ./mywatch.sh ‘”ruby script/spec spec/models/article_spec.rb —format specdoc”’ spec/models/article_spec.rb app/models/article.rb
  echo ^C quits
else
  command=$1
  shift
  touch /tmp/old_ls.$$
  while :
  do
    ls -l $* > /tmp/new_ls.$$
    diff -q /tmp/new_ls.$$ /tmp/old_ls.$$ || eval $command
    mv /tmp/new_ls.$$ /tmp/old_ls.$$
    sleep 3
  done
fi

Footnote:

Found a Ruby way of doing this http://nubyonrails.com/articles/automation-with-rstakeout also added some code that will use the windows Snarl, rather than the Mac growl to send messages and a one line script  for people who are lazy enough to not want to type long commands, see this.

Railscon 2007 Berlin

I originally started a post about the conference but it got too old and Blog City deleted it. Never mind. Have another go.

The conference was fantastic and I enjoyed every minute of it. The presentations are here.

I went to the charity tutorial on testing, which was hard core but really good. If you want to see what it was like have a look at Ruby Hoedown and click through to the testing tutorial. I’ve watched all of the content from this conference – very good.

By far the worst talk of the whole show was the one by the inventor of REST Roy T. Fielding, Chief Scientist, Day Software, who just read his very dull powerpoint to us and did’t say anything new or useful in language I could understand. This was annoying because the ideas are really good – just expressed in a very closed, boring and inarticulate way.

The talk from Cyndi Mitchell of Thought Works was by far the most interesting visually, she presented using hand-drawn diagrams rather than the usual bullet point bullshit, and what she had to say was interesting and well thought out.

I was most moved by the Rails in Africa talk, and resolved to try and help them. Of course, one month or so later I’ve done nothing about it because I’ve been running around like a headless chicken. 

I met up with some of the guys from the North West Ruby User group, and also eventually got pointed to geekup (another North West England thing). I can recommend Sushi and German beer.

Need to post more on Geekup, the Manchester Ponto de Cultura , learning to use RSpec,  paddling the Washburn, using SVNRepository for my own projects  (only $4 a month for peace of mind – bargain) – just to name a few.

AND … getting into podcasts after being so sniffy about them. (list of good podcast sites to follow)

AND … Going to see my dharma teacher after several years (next week). But that’s pretty private.

Onward