Posts Tagged ‘TDD’

Testing route helper methods and formatted routes

Saturday, February 7th, 2009

Here’s a quick tip on how to test those helpers such as ‘new_post_path’ and ‘home_path’. Also those formatted routes such as ‘formatted_posts_path’. And why is important to test those.

Here’s how I test route helper methods.


# spec/controllers/ideas_controller_spec.rb

describe IdeasController do
  describe "GET /ideas/new" do
    it "should have helper method new_idea_path" do
      get :new
      new_idea_path.should_not be_nil
      new_idea_path.should == '/ideas/new'
    end

    it "should have helper method formatted_new_idea_path" do
      get :new, :format => :json
      formatted_new_idea_path.should_not be_nil
      formatted_new_idea_path.should == '/ideas/new.json'
    end

    (...)
  end
end

And why it’s important to test those methods, since they’re already tested by Rails tests?

Rails 2.3 has removed all those formatted_xxx named routes. Turns out these routes ate up a lot of memory and served minimal purpose. Read article here.

So if you use something like formatted_new_user, and you need to upgrade the application to Rails 2.3, it would be broken without test coverage.

Cucumber generic method for handling Webrat visit method

Saturday, February 7th, 2009

Here’s a tip for creating a generic Cucumber step, handling the Webrat visit method.

Imagine you have many Cucumber scenarios, and you have many steps like these:


  Given I am on the new task page
  Given I am on the new project page
  Given I am on the home page

This way you’d have to create mapping between these strings above, to real paths inside the application. You could use the method ‘path_to’, which is already provided with Cucumber, inside features/support/paths.rb. That could lead to something like this:


# features/support/paths.rb

def path_to(page_name)
  case page_name

  when /new task/i
    new_task_path
  when /tasks/i
    tasks_path

  when /new user/i
    new_user_path
  when /users/i
    users_path

  when /home/i
    root_path

  # and so forth...

  else
    raise "Can't find mapping from \"#{page_name}\" to a path."
  end
end

Or you could use something more generic and more maintanable.

So, let’s take advantage of the path helpers already provided by Rails such as new_task_page, tasks_path, home_path, and do this instead:


  Given I am on the new_task page
  Given I am on the new_project page
  Given I am on the home page

And add this to your create_task_steps.rb:


# features/step_definitions/create_task_steps.rb

Given /^I am on the (.+) page$/ do |page_name|
  eval("visit #{page_name}_path")
end

This way Webrat will visit the path: new_page_path, and you won’t have to maintain the paths.rb file.

The RSpec Book is in beta!

Thursday, January 29th, 2009

A whole lot of Ruby developers are waiting for this book. It’s finally in Beta state, which means you can buy the PDF version and download new versions as the book is being written. For only US$ 24 it’s a bargain!

Title: The RSpec Book: Behaviour Driven Development with RSpec, Cucumber and Friends

350 pages, Apr 2009

Authors: by David Chelimsky, Dave Astels, Zach Dennis, Aslak Hellesoy, Bryan Helmkamp, Dan North.

ISBN: 9781934356371

Is your team trying to do TDD and failing? Are you finding your test suites bloated and difficult to read, understand, or maintain? Business applications today are plagued with features that are never used, highly coupled code that is hard to change, and expensive test suites that aren’t run any more because they are brittle and unreadable.

RSpec, Ruby’s leading Behaviour Driven Development tool, helps you do TDD right by embracing the design and documentation aspects of TDD. It encourages readable, maintainable suites of code examples that not only test your code, they document it as well. The RSpec Book will teach you how to use RSpec, Cucumber, and other Ruby tools to develop truly agile software that gets you to market quickly and maintains its value as evolving market trends drive new requirements.

How To Love Tests

Friday, November 21st, 2008

On this presentation “How I Learned to Love Testing”, Gregg Polack talks about:

  • Why would you test your code?
  • When do you test your code?
  • The path to developer enlightenment
  • Test Driven Development (TDD)
  • Behavior Driven Development (BDD)
  • Where’d RSpec come from?

The original post here, and the link to download the entire presentation with better quality than Vimeo’s.