Archive for the ‘Development’ Category

Mock testing with mocha

Wednesday, September 12th, 2007

Mocha is a library designed to let you mock objects in your test environment. It’s very useful for testing around external dependencies

Inside one of my controllers I am calling a SOAP Service:

message_result = SOAPService.send_message(message)

This leaves me with a problem in my functional test - external dependencies are not conduive to self-contained test. Enter Mocha to the rescue!

Mocha lets me create a stub for a method on any instance of my object. This means that I can intercept the calls in my test, without needing to touch the code inside my controller.

SOAPService.any_instance.stubs(:send_message).returns( MessageResult.new )
get :respond
assert_match(/1<\/status>/, @response.body)

The get :respond call above would normally invoke a SOAP call, but using any_instance.stubs(:send_message).returns( MessageResult.new ) means that the SOAPService.send_message method now will return a new MessageResult. My controller doesn’t care and now my functional test is tight and focussed, and tests the controller logic without the external dependency being a factor.

Did you find this Ruby on Rails article useful? Why not recommend Toby at Working With Rails?

RailsRumble: distro decisions decisions

Friday, September 7th, 2007

I received my Linode account for the Rails Rumble.

I logged in and the first thing I have to do is pick the Linux distro I want to install and use for the Rumble.

There are far too many options!

The available distros are:

  • Arch 0.7.1
  • Debian 4.0 (Etch)
  • Finnix 86.1
  • Ubuntu 6.06
  • Ubuntu 7.04
  • CentOS 5.0 (RHEL rebuild)
  • Fedora Core 6 (FC6)
  • Slackware 12.0 (-new-)
  • Mandrake 9.1 (Bamboo)
  • Gentoo Linux 2006.1

I’m not really a systems person … I know my way around Linux, but I don’t have a whole lot of experience setting a machine up from scratch. I’ve been lucky that for the last several years I’ve worked with truly awesome sys admins who take care of all this stuff for us.

So what to use?

I am leaning toward Ubuntu 6.06 (Dapper Drake) because it looks like there are some good tutorials around (I even think the Peepcode guys have been using Ubuntu for some of their work - it might be time to subscribe).

Any ideas are appreciated. I am definitely going to learn a lot this weekend.

Facebook Apps are very hard to debug

Tuesday, September 4th, 2007

I am working on a Facebook Application for a client and although the Facebook API itself is rich and elegant, debugging the app is very difficult. Facebook uses it’s own markup called FBML, which is pulled from your app, translated into HTML and Facebook commands and then displayed. This means that applications have to actually run on a remote server that is accesible by the Facebook back-end.

The development process reminds me of the halycon days of my J2EE days (test,fix,build,jar,war,ear,undeploy,waitdeploy,wait,swear):

  1. Make minor change in code
  2. Push the change to the server
  3. Undeploy the application from the test Facebook profile
    1. Remove application
    2. Delete all sessions and cookies
    3. Clear the local database
  4. Redeploy the application
  5. Find another ******** problem!
  6. Repeat

There has to be an easier way. I am going to look at some DNS magic so I can develop on my local machine and point Facebook to it. Problem with that, of course, is you need to reconfigure the application so the client can see it on a public server. Or run your dev box as public a server.

RailsRumble Notes - WoR! by self.Team

Saturday, September 1st, 2007

You can find all my notes for the RailsRumble in Backpack.

I changed my mind, I am going to keep my notes here in the blog.

Go self.Team!

How not to hire …

Friday, August 31st, 2007

37 Signals has a post about Writing Better Help Wanted Ads, which has some excellent advice and a summary of some ideas from around the blogospehere.

I have had some pretty bad (and sometimes actually rude) encounters recently as a Freelance Developer.

A fairly typical example was this response to my considered, thoughtful and detailed application for an advertised position:

I am looking for freelance web programmers who are proficient in the
following:
-HTML, CSS, JavaScript and Flash up to $20/hour
-with PHP, MySQL and Linux, and willing to learn Ruby on Rails up to
$22/hour
-with Ruby on Rails _experience_ up to $25/hour

I prefer programmers with Ruby on Rails experience, but will consider
you if you have _very_ strong skills in the first two items.

You must be able to work under tight deadlines. I prefer people who are
proactive and have a good design sense. You must be a proficient
programmer. You must be willing to take design direction, and work under
an established set of procedures. Work availability varies.

I read this as:

  1. You must be cheap
  2. You must have lots of experience
  3. You must not value that experience (see points 2 via 1)
  4. You must work really hard and under pressure
  5. You must do what we tell you
  6. You still may not get any actual work

There’s no mention of the types of work, the flavour of the projects, or why I might be interested.

On top of all this, it was instantly evident that the responder hadn’t read my application - I had actually detailed my most recent experience with Rails (and other relevant technologies) on several “real-world” projects.

The fact is:

If you are good at your job, you can choose the work you do.

Finding good people is hard, regardless of industry, but particularly in software development. The employment or negotiation process is as much about the potential employer selling the role as it is about me proving I am good at what I do.

I’m certainly not saying you need to treat me like a God of Code, but perhaps you should actually read my job application …

self.Team

Wednesday, August 29th, 2007

I’ve registered for the RailsRumble as a one-person team called, cunningly: self.Team

See what I did there?

I am going to be building a game - a browser-based, AJAXified wargame loosely based on the Roll Out The Gun Barrels ruleset. I’ll be posting a few design ideas over the next week.

Ready to Rumble

Monday, August 27th, 2007

Rails Rumble has been announced. 48 hours to build an application from scratch.

I am going to enter … as a solo developer. I think I will attempt to build the game I have been occasionally designing for the last couple of months. Or maybe my idea for a Google Adwords Landding Page Manager. I’ve already signed up for the AdWords API …

Rake Task for MySQL Backup to Amazon S3

Saturday, August 25th, 2007

There are a couple of these around, but I rolled my own because I wanted something quite simple.

# S3 Backup Task for MySQL
# Assumes InnoDB tables
# Database User needs the "reload" permission on the database (for --flush-logs in mysqldump)
#
# Stores files in Amazon S3 using the excellent AWS Gem: http://amazon.rubyforge.org/
# For information about Amazon S3: http://aws.amazon.com/s3
#
# Installation
# 1) Install AWS Gem
# 2) Enter your S3 Bucket, access_key_id and secret_access_key in this file
# 3) Run (rake db:backup)
#
# Inspired by code from:
#   http://blog.craigambrose.com/articles/2007/03/01/a-rake-task-for-database-backups
#   http://www.rubyinside.com/advent2006/15-s3rake.html

namespace :db do
  require "aws/s3"

  desc "Backup database to Amazon S3"

  task :backup => :environment do
    BUCKET = "bucket"

    begin
      AWS::S3::Base.establish_connection!(
        :access_key_id     => "access_key_id ",
        :secret_access_key => "secret_access_key "
      )

      db_config = ActiveRecord::Base.configurations[RAILS_ENV]

      backup_path = "db/backup"
      File.makedirs(backup_path)

      datestamp = Time.now.strftime("%Y%m%d%H%M%S")
      file_name = "#{RAILS_ENV}_#{db_config['database']}_#{datestamp}.sql.gz"

      backup_file = File.join(backup_path, file_name)

      sh "mysqldump -u #{db_config['username']} -p#{db_config['password']} --single-transaction --flush-logs --add-drop-table --add-locks --create-options --disable-keys --extended-insert --quick #{db_config['database']} | gzip -c > #{backup_file}"
      puts "Created backup: #{file_name}"

      puts "Storing file in S3: #{BUCKET}"
      AWS::S3::S3Object.store(file_name, open(backup_file), BUCKET)

      puts "Backup Complete"
    rescue AWS::S3::ResponseError => error
      puts error.response.code.to_s + ": " + error.message
    end

  end

end

Managing Amazon S3 on Mac OS X

Friday, August 24th, 2007

While we’re talking Amazon S3, I found the Mac OS X S3 Browser, it works really well for managing your S3 account.

Speaking at Melbourne Ruby User Group

Friday, August 24th, 2007

I’m speaking at the Melbourne Ruby User Group next week. Either on using Amazon Web Services. or hacking with Prototype (probably focussed on the new 1.6.0 features).

Lineup & Location

ThoughtWorks
Level 11, 155 Queen St

  • Pete Yandell - SpiffyAttributes
  • Clifford Heath - HAML
  • Mike Bailey - Jester
  • Ben Schwarz  - JS Hacking
  • Toby Hede - Amazon AWS and/or Prototype Hacking
  • Marty Andrews - Logging and/or the Rails Plugin Architecture