Monday, June 6, 2011

Spec it or not?

I had a conversation with my friend Joe Fiorini a couple of days ago about this particular code:
module ApplicationHelper
  def gravatar_for(email)
    image_tag Utils::Gravatar.for(email), size: "30x30"
  end
end
He showed me this code and my first question was: "Did you write tests for it?" He said: "Why? This is a simple method. What should I test?"
We both agreed that there isn't much that could go wrong there.

But:
  1. Writing a spec against this code is quick and easy.
  2. I'd much rather look at a spec documentation that describes this code than the code itself.
  3. A spec describes intent.
It took me about 4 minutes to write this spec:
require 'units/spec_helper'
require 'application_helper'

describe ApplicationHelper do
  context "#gravatar_for(email)" do
    specify "provides an image tag with Gravatar url" do
      dummy = Object.new.extend ApplicationHelper # It's a module

      Utils::Gravatar.stub(:for).and_return("some url")
      dummy.stub(:image_tag).and_return("some image tag")

      dummy.gravatar_for("some_email").should == "some image tag"
    end
  end
end
I am not using mocks, I like to keep my tests "loose". I want to make sure when I call this helper I get the result I expect through the canned responses provided by the stubs.

The image_tag helper needs a url. This url is provided by a utility class method, stubbed out on line 9.

It's important to mention that I am not testing Rails' image_tag helper. I leave that to the Rails core developers and contributors. I want to make sure that the method image_tag is recognized in the given context and it returns the string I expect.

Once I execute the spec, this is the output:
ApplicationHelper
  #gravatar_for(email)
    provides an image tag with Gravatar url

Finished in 0.00151 seconds
1 example, 0 failures
What did I mean by "communicating intent"?
Let's say a new developer comes to the team and decides to change the gravatar_for method like this:
def gravatar_for(email)
  #image_tag Utils::Gravatar.for(email), size: "31x30"
  url_for some_kind_of_named_route(email)
end
As soon as he runs the spec the error is obvious:
F
Failures:

  1) ApplicationHelper#gravatar_for(email) provides an image tag with Gravatar url
     Failure/Error: dummy.gravatar_for("some_email").should == "some image tag"
     NoMethodError:
       undefined method `some_kind_of_named_route' for #<Object:0x01008e8838>
     # ./app/helpers/application_helper.rb:7:in `gravatar_for'
     # ./spec/units/helpers/application_helper_spec.rb:12:in `block (3 levels) in <top (required)>'

Finished in 0.00139 seconds
1 example, 1 failure
To me this is a good warning sign that suggests the following: "You can change the method behavior, but the original developer meant it the way it's described in the spec. Now please fix the spec so it's green again. Oh, and make sure you run the full stack automated acceptance tests before you push your code."

Knowing metaprogramming, stubbing and mocking makes it easy to write specs.
I would have a totally different opinion if it took a lot more code and ceremony to do it.

Friday, April 15, 2011

Running Rails Rspec Tests - Without Rails

I opened up my twitter client this afternoon and I saw "54 Messages, 28 Mentions". I tell you honestly, the first thought I had was: my twitter account had been hacked. Then I started to comb through the messages and I found out what happened. It all started with a tweet from Joe Fiorini.


We both worked together on a large Rails application. The application was a little light on tests, so I asked the other developers why they are not writing more specs? The answer was all too familiar: "it just takes forever to run them". Yup, Rails had to load up, schema needed to be verified, the entire universe had to be included and 30 seconds later our specs were executed.

We started creating POROs - Plain Old Ruby Objects - as pure services and put their RSpec tests into APP_ROOT/spec/units directory. Our goal was to keep the execution time under or around 2 seconds. Sure, it's easy when you don't have to load Rails controllers or active record models. But what happens when you have to?
This post will explain that.

The controller I used for this example is simple:
class TracksController < ApplicationController
  def index
    signed_in_user
  end

  def new
    @track = Track.new
  end

  def create
    feed = params[:track]["feed"]
    @track = TrackParserService.parse(feed)

    unless @track.valid?
      render :action => 'new'
      return
    end

    @track.save_with_user!(signed_in_user)

    render :action => 'index'
  end

  def destroy
    Track.find(params[:id]).destroy

    @user = User.first
    render :action => 'index'
  end

  private

  def signed_in_user
    # No authentication yet
    @user ||= User.first
  end
end
The first controller action I wanted to test was "index".

I created the directory structure APP_ROOT/spec/units/controllers and saved my file in this directory under the name tracks_controller_spec.rb.

I started out with this code:
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", ".."))
$: << File.join(APP_ROOT, "app/controllers")

require 'tracks_controller'

describe TracksController do
  
end
You could move the first two lines into a spec_helper, I wanted to keep it here for clarity.

I received the following error:
`const_missing': uninitialized constant Object::ApplicationController (NameError)

No worries: TracksController inherits from ApplicationController, it's part of my app, I just had to require it.
require 'application_controller'
And the error:
`const_missing': uninitialized constant Object::ActionController (NameError)

This was the point where I had to require Rails.

Instead of doing that, I just defined the class myself so the controller was aware of it. I also needed to declare the class method "protect_from_forgery", but I left the implementation blank. Please note that the class declaration is above the require statements.
Here is the entire spec after my changes:
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", ".."))
$: << File.join(APP_ROOT, "app/controllers")

# A test double for ActionController::Base
module ActionController
  class Base
    def self.protect_from_forgery; end
  end
end

require 'application_controller'
require 'tracks_controller'

describe TracksController do
  
end
Running the spec:

Finished in 0.00003 seconds
0 examples, 0 failures

The first test just ensures that the User active record model will load the first user if the @user instance is nil.
describe TracksController do
  let(:controller) { TracksController.new }

  specify "index action returns the signed_in_user" do
    # setup
    user = stub
    User.stub(:first).and_return user

    # execute action under test
    returned_user = controller.index

    # verify
    returned_user.should == user
    controller.instance_variable_get(:@user).should == user
  end
end
The test is straightforward. User model is returning a stub - I don't really care what that returned object is, I just check if they're the same object. In the verification part I made sure that the instance variable was set properly. Great that your can check an un-exposed field on a object with a little bit of metaprogramming?

I executed the spec and received the following error:

Failures:

  1) TracksController index action returns the signed_in_user
    Failure/Error: User.stub(:first).and_return user     NameError:       uninitialized constant RSpec::Core::ExampleGroup::Nested_1::User

Well, I need to require the User model to fix this error. Or do I? I am not using any functionality of the User class - whatever I am using is stubbed out. I just defined the class without any implementation.

This line was added to the spec right above the describe block.
class User; end
I execute the test and it's all green.

TracksController
  index action returns the signed in user

Finished in 0.00079 seconds 1 example, 0 failures 1.26s user 0.28s system 99% cpu 1.546 total

1.5 seconds is not all that bad to run a controller action test.

Let me describe how I tested the "create" action.
Take a look at the controller code above and review what it does. The @track instance is constructed by the TrackParserService class' parse method. Then active record validates it and if the model is invalid the controller's "new" action is rendered.

Here is the spec for that:
context "when the model is not valid" do
  it "renders action => 'new'" do
    # define a method for params - TracksController is unaware of it
    controller.class.send(:define_method, :params) do
      {:track => "feed"}
    end

    track = stub(:valid? => false)
    TrackParserService.stub(:parse).and_return(track)

    render_hash = {}
    # hang on to the input hash the render method is invoked with
    # I'll use it to very that the render argument is correct
    controller.class.send(:define_method, :render) do |hash_argument|
      render_hash = hash_argument
    end

    controller.create

    # verify the render was called with the right hash
    render_hash.should == { :action => 'new' }
  end
end
I used Ruby's metaprogramming again to set up the params hash. It really doesn't matter what's in it, since I stub out the TrackParserService. The method "render" comes from Rails, I had to define that as well. Please note that I record what the render method was invoked with, this way I can verify that the input hash was correct.
I also had to define - with no implementation - the Track and TrackParserService classes.

When I executed the specs, all of them passed:

TracksController
  index action returns the signed in user
  new action returns an instance of Track
  when the model is not valid
    renders action => 'new'

Finished in 0.00203 seconds
3 examples, 0 failures
bundle exec rspec spec/units/controllers/tracks_controller_spec.rb -fd 1.32s user 0.29s system 99% cpu 1.614 total

You can review the entire example in this gist.

This code is rough. I just used it to show you how we try to keep our test execution fast. I acknowledge that I am doing some very dangerous stubbing here. However, I have the higher level cucumber tests to protect me against unexpected errors.

I can't tell you what it means to run all of my 150+ specs within 2 seconds. I think it's a little bit of an extra work, but it's well worth the effort!

Sunday, April 10, 2011

Rapid Feedback

I am not a WPF expert. In fact, I don't know it well enough. But let me tell you what it was like working on a WPF project last year.


It was 10 o'clock in the morning and I sat in my cubicle. I had to pull the latest changes from the source control server. I had to run a couple of batch files to compile all the code that took about 3 minutes. In the mean time I fired up Visual Studio 2010 and ran my latest unit tests to make sure everything was in good shape.

I started up the WCF services, 86 of them, and a little later I was ready to run the UI app. It took another 30 to 40 seconds to load and get to the login screen. I logged in and selected from the menu where I wanted to get to. That page was a list of items, I had to select one of them just to get to the detail page. Finally, I was there!

Let me sum it up:
* 180 seconds to compile the app
*   50 seconds to fire up all WPF services
*   20 seconds to start the WPF UI App
*   60 seconds to log in and go to the page I had to modify
TOTAL: 310 seconds

My task was adding a new TextBox to this page. Simple. I opened up the XAML file which was an ugly xml file with weird namespaces and special attributes all over. I grabbed a TextBox XAML code from somewhere, pasted it in, made sure all the namespaces were fine and I was ready to run it.

I had to shut down the UI app, compile the UI project, start it up again, log in, select the menu option to get to the list page and choose one item to see the detail.

Here is how long this took:
* 30 seconds to compile the UI app
* 20 seconds to start the WPF UI App
* 60 seconds to log in and go to the page I had to modify
TOTAL: 110 seconds

And it turned out that I did not set up the Grid for this TextBox properly, so I had to do some minor tweaks to the XAML page. I did that, killed the UI app, complied the code, ran the WPF UI app, logged in, went to the page and 110 - or one hundred and ten - seconds later I verified that all look good.

But this was the fast part. Once I had all the UI set up properly, I had to get under the hood and modify the domain object. The change was "simple": just add a text field to the database, modify the domain object, set up the NHIbernate mapping, change the Data Transfer Object, add this field to it and set up its mapping if I had to.
Now to make sure all this worked I had to shut down the UI app, the WPF services. Compile the code, regenerate the NHibernate mappings, fire up the WPF services, run the UI, log in, select the page and pick an item to get to its detail. Simple, right?

Here is the break down:
* 30 seconds to compile the Data Access Code
* 30 seconds to regenerate the NHibernate mapping xml
* 50 seconds to fire up all WCF services
* 20 seconds to start the WPF UI App
* 60 seconds to log in and make sure that all looks good
TOTAL: 190 seconds

Wait! 3 minutes just to see if everything is working properly?

Give me a break.

What company with a tight budget and ever approaching deadlines could afford spending 3 minutes just to see if a simple change is functioning properly or not? Who would dare to touch the existing code to clean it up a bit?

One of the great things I like about working with Ruby and Rails is the rapid feedback. No, I am not talking about how long it takes to execute my - Rails disconnected RSpec - tests. (I'll try to write about that in an upcoming post.) I just change the code, hit the browser's refresh button and about 5 seconds later I have the page loaded, the session preserved and I have the answer.

I am talking about 5 seconds and not a couple of minutes.

Wednesday, February 16, 2011

First Month Without Windows

It's been a little over than a month since I left the Microsoft Universe behind. I had used OS X and Linux before - mostly in the evenings and weekends - but I made the jump finally and I only touch Windows when I browse the web on my wife's laptop. And I just couldn't be happier...

Life on the Mac

I have a pretty powerful Mac. It has an Intel Core i5 CPU and I upgraded the RAM from 4GB to 8GB as soon as I received it. I don't have an SSD just yet, but I am still pretty happy with its performance. Whenever I feel like checking the current state of the machine I just run "htop" in the terminal.

I started out using Firefox for my work email, but I write automated tests in it as well and I had to find another solution. "Before I ran out of browsers" I investigated my options. I could have purchased Mailplane but I did not feel I needed a full blown app for it. I found Fluid, a site specific browser. Now I can open my work email just like any other application and I am still using a browser inside. I found a nice PNG file that I set up with it and the app looks just like a native app when I tab between applications. Here is how it appears under the Applications folder:

To organize my thoughts and notes I started using Evernote. It's a great tool for note taking but I think it does an even better job at organizing them. I even installed the Evernote Chrome extension:

I don't use the mouse - or track pad - to launch an application. First I used Spotlight but switched to Alfred App recently. It's fast and I can use keyboard shortcuts. This image tells it all:

Living my life in the Terminal (iTerm)

After using the terminal for a couple of weeks I switched to iTerm2. I don't use all its neat features just yet but I do like the split view mode. I have cucumber features running on one side and rails logs on the other.

I am still learning (who doesn't) and tweaking all the different configuration options of my ~/.vimrc and ~/.zshrc files. I am particularly happy with this addition to my ~/.vimrc file that ignores my arrow keys when I am in command mode. No more arrows to move around!

" Ignore arrow keys in vim
:map <Left> <Nop>
:map <Right> <Nop>
:map <Up> <Nop>
:map <Down> <Nop>
:map <PageUp> <Nop>
:map <PageDown> <Nop>
:map <Home> <Nop>
:map <End> <Nop>

:map! <Left> <Nop>
:map! <Right> <Nop>
:map! <Up> <Nop>
:map! <Down> <Nop>
:map! <PageUp> <Nop>
:map! <PageDown> <Nop>
:map! <Home> <Nop>
:map! <End> <Nop>

I even started listening to Pandora in the terminal through Pianobar.

I have used Git before, but my skills are not where it should be. I started reading the book Pragmatic Version Control Using Git which I'd recommend to anybody who wants to go deep with Git. You should also check out the great Git Immersion class created by EdgeCase.

My Typing Sucks

Well, maybe it's not that bad, but my typing could and should improve. It's just not fast enough and I don't use all my fingers. I need to take my eyes off the monitor and look at the keyboard when I try to use special key commands that I have not used much before. And my typing is not accurate, I frequently have to go back and fix words that I mistyped.
Unacceptable. I wonder if companies should check in an interview how well a candidate can type.

I used the web site typingweb.com and an app called aTypeTrainer4Mac to practice. I am still not where I'd like to be, but I am working on it.

Thanks to Joe Fiorini for showing me endless tips mentioned in this blog post.

Sunday, December 26, 2010

Ruby Mocks vs Stubs - CleRB Presentation

I talked to somebody a while ago about a line of code that had tremendous beauty in my eyes:
setup_response = stub(:token =>'xyz_some_token')
This might seem strange to someone new to dynamic languages. The stub object returns the string 'xyz_some_token' when the "token message" is sent to it. I have no idea - and I don't really care - what type of object it is. What really matters is that is has a canned response for the "token message".

He suggested that I should do a talk on this. I submitted the idea to our local Ruby user group and Michael "Doc" Norton - the user group organizer - asked me to present it.

Preparing for a presentation is hard - takes time and effort - but I learned so much from it that I would and I will do it again!

I used the Order - Warehouse example from Martin Fowler's Mocks Aren't Stubs writing. I also wrote a Twitter client where I used mocking/stubbing in the controller tests and Fakeweb to stub out http calls from Cucumber.

After the talk we had the following conclusions:
* Although Stubs are not as sophisticated as mocks, they are really powerful and reflect clean code
* Try to use stubs over mocks
* Abused mocking could be a code smell -> introduce abstraction and use stubs

The examples from the talk are in my github repository.


I'd like to thank Joe Fiorini for meeting with me a couple of days before my talk. He had great ideas that I used in my presentation. Thanks for it!

Saturday, November 13, 2010

Running Jasmine BDD Specs in the Terminal

I had a pretty bad week with Ruby on Windows 7 64 bit: I tried to set up DBI and ODBC on my work machine but I did not have any luck with that.

I needed something to feel good about, so I decided to set up Jasmine BDD spec execution in the terminal on OS X.

I downloaded the standalone zip file from Jasmine's web site and made sure that the sample specs are executing fine with the SpecRunner.html file in the browser.


I wanted to execute the exact same specs but instead of running it in the browser, I wanted to do it in terminal.

Michael Hines' blog post was a pretty good starting point. He used JazzMoney, so I tried it myself.

I easily found JazzMoney's installation instructions on their github page.


It has prerequisites: I had to install harmony first.

I picked ruby-1.9.2-head from RVM, created a new gemset called "jasmine" and I got started.
Harmony has dependencies as well, I had to get stackdeck and johnson before I installed harmony.
$ gem install stackdeck
$ gem install johnson -v "2.0.0.pre3"
This is where it turned ugly. Stackdeck got installed fine, but johnson had some issues.

Building native extensions. This could take a while...
ERROR: Error installing johnson:
ERROR: Failed to build gem native extension.

After Googling the error I found out that johnson is not playing nice with Ruby beyond 1.8.7. I went back to RVM and started out by installing a new version of Ruby.
Here is what I did:
$ rvm install 1.8.7-p249
$ rvm 1.8.7-p249
$ rvm gemset create jasmine
$ rvm 1.8.7-p249@jasmine // switched to jasmine gemset
$ gem install stackdeck
$ gem install johnson -v "2.0.0.pre3"
$ gem install harmony // I tested harmony with a quick test in IRB, worked fine
$ gem install jazz_money
I did not have any problems with installing the gems under 1.8.7.

I had to create a Ruby script that sets up the test suite and this is the file I ran in the terminal. My run_specs.rb file was placed into the root folder right next to SpecRunner.html:
require 'rubygems'
require 'jazz_money'

javascript_files = [
  'spec/SpecHelper.js',
  'spec/PlayerSpec.js'
]

jasmine_spec_files = [
  'src/Player.js',
  'src/Song.js'
]

JazzMoney::Runner.new(javascript_files, jasmine_spec_files).call
I ran the file with the following parameters:
$  ruby run_specs.rb -f n -c
Success! This is the output I received in the terminal:


I did one more thing: I created a shell script, this way I did not have to remember all the command line arguments.
I saved this in the specrunner.sh file:
ruby run_specs.rb -f n -c
I can now invoke my specs by running "sh specrunner.sh" in the terminal.

Tuesday, November 9, 2010

JavaScript Closures for DRY-ing Up The Logic

Our #Hackibou today was a real blast. We focused on JavaScript development using the great Jasmine BDD framework. Since most of us were a little rusty on JS, we decided to start with something very simple: a Calculator. Our task was to develop a calculator that accepts an array of integer numbers in its add(), subtract(), multiply() and divide() functions.

We started out with a couple of very simple specs:
describe("Calculator", function() {
  var calculator;
  beforeEach(function() {
   calculator = new Calculator();
  });

  describe("Arithmetic operations on an array input", function() {
   it("creates a new Calculator object", function() {
    expect(calculator).not.toBeNull();
   });

   it("adds two numbers together", function() {
    expect(calculator.add([1,0])).toEqual(1);
   });

   it("adds three numbers together", function() {
    expect(calculator.add([1,2,3])).toEqual(6);
   });

   it("multiplies two numbers", function(){
    expect(calculator.multiply([1,2])).toEqual(2);
   });
  });
});
And our - not too elegant - solution was this:
function Calculator() {
  this.add = function(input) {
    var result = 0;
    for(i = 0; i<input.length; ++i) {
      result += input[i];
    }
    return result;
  }

  this.multiply = function(input) {
    var result = 1;
    for(i=0; i<input.length; ++i) {
      result *= input[i];
    }
    return result;
  }
}
Look at the code above. 90% of the code is duplicated there. One of us suggested assigning the first element of the array to the result right on the declaration. With this change the only difference between the two functions is the operation. One uses addition and the other multiplication. I played with JS closures a little bit before, so I proposed this:
function Calculator() {
 var operator = function(result, input) { return result + input; };
 this.add = function(input){
  return operation(input, operator);
 };

 this.multiply = function(input){
  return operation(input, function(result, input){return result*input;});
 }

 function operation(input, operator) {
  var result = input[0];
  for(i = 1; i < input.length; i++){
   result = operator(result, input[i]);
  }
  return result;
 }
}
Check out the operation() function. It uses two parameters, the first one is the array of integers and the other is a function object that holds the calculation logic. It's invoked on line 14. The variable result is both passed in as the first input and is assigned as the result of the function call. One of us suggested using the shift() function on the input array, this way we did not have to start our for loop with the second element of the array. Our operation() function now looked like this:
function operation(input, operator) {
 var result = input.shift();
 for(i = 0; i < input.length; i++){
  result = operator(result, input[i]);
 }
 return result;
}
Adding subtraction and division was very simple:
this.subtract = function(input){
  return operation(input, function(result, input){return result-input;});
 }

 this.divide = function(input){
  return operation(input, function(result, input){return result/input;});
 }
Please note that there is no if statement in the Calculator object.

Our final solution can be found in this gist.