2

Introducing netrecorder, my first ruby gem.

3 January 2010 in Ruby, Testing

The netrecorder gem works with fakeweb to record your requests and responses automatically. This way you can build your test suite against a live api and just flip a switch to use the local recorded data instead, saving time and unnecessary network calls. Visit the github page to see the source code and learn how to use netrecorder. This is my first gem so I’m eager to hear any feedback. I also wrote a tutorial based on my experience building a gem. Read it here.

Tags: , ,

11

How to build a ruby gem and host it on gemcutter

3 January 2010 in Ruby

This weekend I built my first ruby gem. The stuff out there on the internet is pretty confusing so hopefully this post will make it exceedingly clear. The most complete source I found was the Railscast, “Making a gem”.

Here are the steps I took:

1. Create the hello world library
2. Use the echoe gem to create a rake file that will generate your “gem spec”
yikes! That’s a mouth full.
3. Use rake to create the manifest and gemspec
4. Install the hello world gem locally
5. Create and install a self-signed certificate.
6. Update your bash profile with the certificate information.
7. Use ‘rake gem’ to create the gem
8. Add your project to github
9. Use the gemcutter gem to host your gem on gemcutter
10. The holy grail - gem install hello_world

Create the library

The hello world gem will have one method

HelloWorld.say_hello

This will write out ‘hello world’ when called. We’ll store the method in a module called “HelloWorld”, as suggested in the screencast.

  mkdir -p hello_world/lib
  touch hello_world/lib/hello_world.rb

Add this code to hello_world/lib/hello_world.rb:

  module HelloWorld
    def self.say_hello
      puts 'hello world'
    end
  end

Create a rake file using echoe

There are a lot of gems out there that are made to make gem building easy. I went with what was recommended in the screencast and tried out the “echoe” gem.

gem install echoe
touch hello_world/Rakefile

Add this code to hello_world/Rakefile:

require 'rubygems'
require 'rake'
require 'echoe'

Echoe.new('helloworld', '0.0.1') do |p|
  p.description    = "A gem that illustrates how to build a gem"
  p.url            = "http://github.com/tombombadil/hello_world"
  p.author         = "Chris Young"
  p.email          = "beesucker @nospam@ gmail.com"
  p.ignore_pattern = ["tmp/*", "script/*"]
  p.development_dependencies = []
end

Now you can type

rake -T

and get a bunch of tasks to help you manage your gem.

Create a manifest and gemspec

A manifest just lists which files should be included in your gem and the gemspec has everything gem needs to manage versioning. Echoe is partial to rubyforge, but this didn’t keep me from being able to use it with github.

cd hello_world
rake manifest
rake build_gemspec

Create a certificate

We’ll sign the gem with a self-signed certificate.

  gem cert --build youremail@example.com
  gem cert –add /secret/path/to/gem-public_cert.pem

Important! Move the gem-private_key.pem file to a secure location

Edit your bash_profile

Add these lines to your bash_profile:

export GEM_PRIVATE_KEY='/secret/path/to/gem-private_key.pem'
export GEM_CERTIFICATE_CHAIN='/secret/path/to/gem-public_cert.pem'

And type this:

source ~/.bash_profile

Install your gem locally

Let’s make sure the gem works. Running this command will install your gem on your computer so you can test it.

rake install helloworld.gemspec

start irb and test

  irb
  require 'rubygems'
  require 'hello_world'
  HelloWorld.say_hello
  # => hello world

It works!

Create the gem

Now we’re getting close. This command will build the gem with your certificate.

rake gem

Add the project to git

Create the repository on github

  git init
  git add .
  git commit -m "initial commit"
  git remote add origin git@github.com:tombombadil/hello_world_gem.git
  git push origin master

Host your gem on gemcutter

Create a gemcutter account

gem install gemcutter
gem push hello_world-0.0.1.gem

The Holy Grail

gem install hello_world

Tags: , , , ,

0

DEMO!

18 December 2009 in Agile Practice, For the Love of Product Owners, Musings, Scrum

At Integrum, saying the word ‘demo’ is akin to saying ‘bomb’ at an airport. We take it very seriously. I’m not talking about a developer demoing to you, the product owner (product owner is what we call the people who pay us at Integrum). We want to work with you over time, to form a true relationship and strive together for mutual success. If you, as a product owner, and we as developers, put out a good demo, then we are likely to be able to keep working together in the future. So, let your developers know well in advance about a demo. We’ll put together a script, we’ll make sure it works perfectly.

Tags:

0

It puts the lotion on its skin

14 December 2009 in Debugging, Ruby on Rails, Testing

Have you ever pulled your hair out trying to find a stray puts statement in your code after a mad night of debugging? Searching for puts across the whole project doesn’t seem to be very helpful. Here’s an easy fix:

Drop this line of code into your environment.rb file and run your tests:

def puts(wheremyputsizat)
  raise
end

That code will override the puts statement and raise an error every time you call puts in your project. You’ll get the file name and line number of each hard-to-find puts statement. Just don’t forget to take that the code out of your environment when you are done.

1

Cucumber Code Ratio

14 December 2009 in Agile Practice, Debugging, Distraction, Testing

I wrote a quick and dirty rake task for grabbing the code ratio for cucumber. Here’s what I got for our one sprint old project:

Code Lines: 379
Test Lines: 431
Code to Test Ratio: 1.1

Update: Here is the ratio from a much larger project posted by co-worker, Clayton Ziingletwitch (thats “Clayton Lengel-Zigich” for those who don’t appreciate childish mockery. But come on Clayton, there’s so few names in the world, it just seems selfish to have so many):

Code Lines: 7443
Test Lines: 9593
Code to Test Ratio: 1.3

Generally I don’t care about test ratio, but I was interested in how many lines of test code it takes go get good coverage with cucumber vs. rspec. We are getting 87.7% code coverage with a 1.1 ratio. With rspec we would be looking at around 3:1 ratio to get similar coverage. Two thirds less test code to get high coverage? I’ll take it.

Here’s the code to generate the stats if anyone wants to use it. Just drop it in your project’s lib/tasks folder. The code assumes the default cucumber features folder setup.

Of course most BDDers will say that it isn’t enough to test with only cucumber - but I’m beginning to think that jumping to unit testing for every bit of code we write is akin to premature optimization. Regardless, I hope that anyone who has been burned in the past and is not testing because they are on a tight deadline: please consider fishing this baby out of the bath water.

Tags: , , ,

2

Left Behind

13 November 2009 in Musings

One time, as a child, I was left behind on a bike ride, and I decided then that I would never knowingly leave someone else behind. I don’t live up to that ideal - but it is my direction, something to orient myself. It is an orientation. Thank you.

When I was a kid, I was definitely a nerd. I was a nerd to nerds, early on. I felt very put off by the popular crowd, you know. The popular crowd is what I called anybody who didn’t like me, come to think of it. Here’s something: In order to feel superior in some way, I decided that intelligence was the most important quality a human could have - better than good looks, for instance, or charisma. If a person didn’t emulate what I thought of as intelligent, I dismissed them wholesale as just another jock, cowboy, stoner, etc. Label, dismiss, repeat. I would actually sit around with my friends and imitate some jock by pretending we were retarded. You know - “Drrrrrruhhhrrh! - that’s you”, hand slapping sloppily against our chests. Can you imagine that? In the end I Left behind those that would leave me behind.

Preemption!

But now I know that intelligence is not the most important quality. Athleticism, for example, is as valuable to an individual as intelligence. The idea of having an innate quality that made me better than the people around me was bullshit. The trick was to put myself into someone else’s shoes and then take a moment to look back at myself. Try it: Put yourself into the shoes of the person that drives you most nuts and look through their eyes back at yourself. Imagine how they see you. If you sincerely do this, you will see that you are the same thing that they are - they are a you from a different angle - a different history, a different culture.

That isn’t to say what we think isn’t important. Or maybe it is to say that - but what we think isn’t unimportant either. What we think is just what we think - whatever we think, it is probably sufficient to maintain our life - our lifestyle - to own a history in this world. That’s true of everybody - the only failures are dead.

But I’m not here to pick on the dead. Best to let the dead lie, I always say - or at least I will from now on. Anyway, this is just something that I think. Nice to share.

Tags: , ,

0

Public Speaking and Retrospectives

9 November 2009 in Agile Practice, Scrum

I recently gave a talk at Desert Code Camp titled “Executable User Documentation: Mapping User Stories to Cucumber Features“.  Well, it wasn’t just me.  I co-presented with Jared McFarland, but lets pretend I did it by myself so I get all the credit, okay?  Here’s something that surprised me: I didn’t feel nervous at all.  I didn’t feel nervous when I recently spoke at the Rails user group meeting about Juggernaut either.  I’ve spoke to groups before, but I’ve always felt nervous, or overly emotional or awkward in some way.  But now suddenly, “Zap!”, gone.  All gone.  Where’d it go?  I figured it out and I am here to share the secret with you:

Facilitate a Retrospective

Every week, (actually, I skipped the last two weeks because I’ve been over stressed (I have felt like a blind person trying to land a 747, and yes, you are now in a nested parenthentical), but let’s just pretend that I never miss retrospectives so that I get all the credit, okay?) Every week, we gather together as a team and try to figure out what we can improve upon and I try to facilitate through some introspection to a small room of a lot of developers who aren’t always interested in the finer points of commitment driven planning.  That’s a hard crowd to play to, let me tell you.  It makes me nervous to the point of sweat - and stink, and yes, I plan on bringing deodorant to retrospectives in the future . . .

Awkward.

So, at the worst, my co-workers want to get back to work, are annoyed at the interruption, feel like it is just a “bitch-fest”, don’t feel like we follow up enough and on and on.  It doesn’t always go this way, of course, but 10 really smart people in a room that could be billing for the time, like i said, it can be a tough crowd.

Compared to what I have to do every Friday at retrospectives, talking about Cucumber at a developers conference is as simple as using Apple’s Time Machine to get the dock back after moving it out of the Core Services folder just to see what would happen.

Just Sayin.

Tags: ,

0

Mapping User Stories to Cucumber Features

8 November 2009 in Agile Practice, Testing

Pick up the deck here. Hope I’ll make time to blog about the experience soon. Had a wonderful time putting it together and presenting it with Jared McFarland over at Bookmans Entertainment Exchange.

Tags: , ,

0

2 rules for contractors

8 October 2009 in About

We, Contractors. Two Rules. Commit to work that will bring maximum value to our clients and learn to meet our commitments.

Maybe three rules. People say rules suck. But what’s Stratego without rules? Monopoly? The rules of the game only suck if you forget you are playing one.

0

Testing javascript prompt popups with Culerity

4 October 2009 in Ruby on Rails, Testing

Solution


And /^I test the prompt/ do
  $browser.webclient.send_remote(:setPromptHandler) ...
         {"lambda {'god help me if this worked'}"}
  And %Q{I follow "hi"}
  Then %Q{I should see "god help me if this worked"}
end

In my previous post, I chronicled the pains of testing javascript popups with Celerity. I thought I had solved my problem until I tried to implement a real test in Cucumber. In my project, I am running standard ruby, but Celerity is jruby. Because you probably don’t want to convert your project to jruby just so you can test javascript a library named Culerity lets you run Celerity without having to run your rails app in jruby. It starts up a separate jruby server process and shells out commands on your behalf. This works pretty well, except of course, when testing javascript prompt popups.

Again, the code in the documentation doesn’t seem to work as advertised, so I had to struggle again with a fix. I’m not going to go into why this works - I’m not totally sure myself. I just want to give you the solution so you won’t have to go through this all over again. I am your testing martyr:


And /^I test the prompt/ do
  $browser.webclient.send_remote(:setPromptHandler) . . .
          {"lambda {'god help me if this worked'}"}
  And %Q{I follow "hi"}
  Then %Q{I should see "god help me if this worked"}
end

Hope that helps someone!

Tags: , , ,

Tweets

  • Tobi:
    ^^ shit, ok now I see it's not your fault, its (my...
  • Tobi:
    Thanks for this great writeup! btw. you missed...
  • Chris Young:
    Thanks for letting me know. I updated the link....
  • maxjgon:
    The link of the project in Github is broken!...
  • Chris Young:
    Oh I see . . . Maybe I should check out this so-c...